Complete Newbie - Please help!

Last post 01-24-2008 1:41 PM by Ted C. 2 replies.

Sort Posts:

  • Complete Newbie - Please help!

    01-24-2008, 12:48 PM
    • Member
      4 point Member
    • VTWP
    • Member since 01-24-2008, 4:23 PM
    • Posts 38

     After years of somehow managing to avoid the migration to .NET it has finally come and I am blown away by the differences between classic ASP and .NET. I'm hoping I can get some help with what should be (and probably is) a simple task: connecting to a MySQL server in my project.

     

    I am running VS 2008 and plan to develop in vb

    I am needing to connect to a MySQL  5.0 database.

    I have both my web project and database on my local computer during development.

    I have installed the MySQL connector 5.1.4

    In Visual Studio 2008 I want to add a Data Connection - how the heck do I go about this? 

    I apologize in advance for such a basic question but everything I have read assumes things like the knowledge of what a "code behind" page is - as in "add this to your code behind page"....

    I'm looking (if possible) to add my data connection via the Server Explorer window in the IDE.

     
    Thanks in advance for any help - I'll keep looking ....
     

     

     

  • Re: Complete Newbie - Please help!

    01-24-2008, 1:39 PM
    • Member
      101 point Member
    • Ted C
    • Member since 01-22-2007, 4:27 PM
    • Posts 113

    First, you should import the appropriate Data namespace.  If you are using OleDb to connect to the database, it would be...

    using System.Data.OleDB;

    ... in C#.  Since you're using VB, I believe that "import" replaces "using".  I believe there is a MySQL extension for ASP.NET that you could instead of OleDb if it's installed.

    You will need to define a Connection object to get to the database...

    OleDbConnection cn = new OleDbConnection();

    cn.ConnectionString = "yourconnectionstring"

    I'm not sure exactly what a MySQL connection string would look like, but I'm pretty sure that you can Google it to find exactly what you need (I found a tutorial at http://aspnet101.com/aspnet101/tutorials.aspx?id=39, incidentally).

     

    With your connection defined, you'll need to create a Command object to hold your SQL query.

    OleDbCommand cmd = new OleDbCommand();

    cmd.Connection = cn;

    cmd.CommandType = CommandType.Text;

    cmd.CommandText = "yourSQLquery"

     

    Then you'll open the connection and execute your query.  The following C# code populates a DropDownList control...

    cn.Open();

    OleDbDataReader rdr = cmd.ExecuteReader();
    while (rdr.Read())

    {

    // loop through list

    ListItem group = new ListItem(rdr["name"].ToString(), rdr["groupID"].ToString());

    lstGroup.Items.Add(group);

    }

    rdr.Close();

    cn.Close();

     

    There are a lot of other classes for manipulating data that you may need depending on what you want to do.  That's just a simple query.  If you want to update or insert data, you'll need to use a DataAdapter class, which will involve a lot more coding and explaining.

    "Here's where the fun begins." -- Han Solo
  • Re: Complete Newbie - Please help!

    01-24-2008, 1:41 PM
    • Member
      101 point Member
    • Ted C
    • Member since 01-22-2007, 4:27 PM
    • Posts 113

    You can find an explanation of connection strings for MySqlConnection connection objects at http://www.carlprothman.net/Default.aspx?tabid=86#MySQLDirectNETDataProvider.

    "Here's where the fun begins." -- Han Solo
Page 1 of 1 (3 items)