Forum Moderators: open

Message Too Old, No Replies

How to connect to MySql with asp.net c#

MySql connection asp.net C# eInfoDesigns

         

Jhet

7:51 pm on Apr 10, 2006 (gmt 0)

10+ Year Member



Can anyone help me with connecting to a MySql database using asp.net c#? I know I must be missing something very simple but I am not getting it. Everything I find on the web only gives me 4 lines of code. Here is what I have:

<%@ Page Language="C#" %>
<script runat="server">
void Page_Load(Object sender , EventArgs e)
{

using eInfoDesigns.dbProvider.MySqlClient;
MySqlConnection oMySqlConn = new MySqlConnection(); oMySqlConn.ConnectionString = "my connection string"; oMySqlConn.Open();

}
</script>

I'm getting an error, Syntax error '(' expected, on the 'using eInfoDesigns.dbProvider.MySqlClient;' line. If anyone could help I would appreciate it. I've been searching for hours for just a simple example of a working connection with a query.

Bluesplinter

1:39 am on Apr 11, 2006 (gmt 0)

10+ Year Member



C# has two different usages of the word "using". The reason you're getting an error, is because you're using it the wrong way, in the right place.

The using statement is a Good Thing for opening database connections, because it will always dispose of the connection when the closing brace is reached. Here's the syntax for this usage:

using (SqlConnection conn = new SqlConnection(myConnectionString))
{
conn.Open();
doSomething(conn);
}

As you can see, this syntax includes parentheses, and is what the compiler thinks you're trying to do, because you have the "using" statement inside the script block.

However, looking at the format of the using statement in your post, that's not where that statement belongs.

In your example "using eInfoDesigns.dbProvider.MySqlClient;" is NOT the same thing as the using statement detailed above. That format is a "Using" directive that tells the compiler which namespace holds the 3rd party classes and methods you want to use. It belongs outside of any classes. However, since your example indicates you are using inline coding rather than code-behind, that directive should be in another format, and *outside* of the script block. Based on your code example, it should appear thusly:

<%@ Page Language="C#" %>
<%@ Import Namespace="eInfoDesigns.dbProvider.MySqlClient" %>

<script runat="server">
void Page_Load(Object sender , EventArgs e)
{

MySqlConnection oMySqlConn = new MySqlConnection(); oMySqlConn.ConnectionString = "my connection string"; oMySqlConn.Open();

}
</script>

Now, this only addresses the compiler error you mentioned... I don't use MySQL from ASP.NET, so I don't claim this code actually will work with MySQL :)

Jhet

1:31 pm on Apr 11, 2006 (gmt 0)

10+ Year Member



Thank you for the reply Bluesplinter! The first thing I had tried was importing the namespace and it didn't work so I had thought maybe I was supposed to be importing something else first or had some syntax wrong. I ended up asking the hosting company what was wrong with the code since they supplied it and they gave me an example of how to connect using and odbc connection intead.

Thanks again! :)