spaceworld
New Member
Hello all,<BR><BR>There seems to little or no information on how to connection to a MySQL database using ASP.NET. If anyone knows how to do this, can you please respond with a concise working example??<BR><BR>Much appreciated,<BR>David<BR>I use a DSN, via an ODBC connection.<BR><BR>******************Example********************<BR><BR><%@ Page Language="VB" debug=true %><BR><%@ Import Namespace="System.Data" %><BR><%@ Import Namespace="Microsoft.Data.Odbc" %><BR><BR><script runat="server"><BR> Sub Page_Load(obj As Object, e AS EventArgs)<BR> <BR> dim Conn as New OdbcConnection("DSN=MySQLDSN")<BR> <BR> dim Cmd As New OdbcCommand _<BR> ("SELECT * FROM pet", Conn)<BR> <BR> dim objReader As OdbcDataReader<BR> <BR> Conn.Open<BR> objReader = Cmd.ExecuteReader<BR> <BR> While objReader.Read<BR> Response.Write(objReader.GetString(0) & "<BR>")<BR> End While<BR> <BR> Conn.Close()<BR> End Sub<BR></script><BR><BR><html><body><BR>Test<BR></body></html><BR>That the Microsoft.Data.Odbc assembly is *not* a part of the default framework, and you have to download and install it seperately.I found the ODBC.NET install on the Microsoft site but it doesn't seem to install correctly.<BR><BR>Error:<BR>"Type 'OdbcConnection' is not defined."<BR><BR>How can I check if the assembly has been installed? Can I install it manually with gautil.exe? If so, how?<BR><BR>Thanks,<BR>David<BR>I installed ODBC.Net but it doesn't seem to install correctly. I manually added the DLL to GAC by doing:<BR><BR>gacutil /i Microsoft.Data.Odbc.dll<BR><BR>It said it was successfully added to the GAC but when I run the code you gave me I receive the following error:<BR><BR>Compiler Error Message: BC30002: Type 'OdbcConnection' is not defined.<BR><BR>Of course I changed the DSN and table name, but it doesn't work. I know the DSN is setup correctly because everything works fine under classic ASP.<BR><BR>Any further comments/suggestions?<BR><BR>Regards,<BR>David<BR>If you don't have one yet, under your Inetpubwwwroot directory names "bin". Copy the Microsoft.Data.Odbc.dll in there.<BR><BR>Put this line at the very top of the page...<BR><%@ Assembly Name="Microsoft.Data.Odbc" %>Xanderno,<BR><BR>Thanks, much appreciated. I finally got it to work. Can you explain to me why those 2 steps were necessary?<BR><BR>Cheers,<BR>DavidBecause,even though the assembly is registered with the framework, it isn't added to the ASP.NET configuration, so, you need the Assembly directive to add it to your page. <BR><BR>We put it in the in folder, because in .NET, now that you don't have to register dlls, it automatically looks in that folder for any private assemblies that you may have added to the application, via the Assebly directive.<BR><BR>There are other ways to get it to work...But that has been the easiest for me so far.