Edit
I clearly do not understand how to do this right. After the examples were provided, I've decided to hit the books a bit more, and try to work it out with the examples given.
Thank you.
End of Edit
I want to connect to my mySql DB, read the table/rows, and write them to the console. Is this code correct? I get the dataset error in Visual Studio 2005.
Code is not mine, got it from the web. I just modified it a little (variable names and such).
If you have a good tutorial to do this, please post the link. =)
/* Performing a SELECT statement using ADO.NET */
#region Using directives
using System;
using System.Data;
using System.Data.SqlClient;
using MySql.Data.MySqlClient;
#endregion
namespace testConnect1
{
class SqlTest1
{
static void Main()
{
string connectionString = "server = localhost user id = root Password = blank database = test1"; //connection string
SqlConnection mySqlConnection = new SqlConnection(connectionString); //creates connection
string selectString = "Select field01, field02, field03 " + "FROM myDataTable"; //selects fields to be accessed
SqlCommand mySqlCommand = mySqlConnection.CreateCommand();
mySqlCommand.CommandText = selectString;
SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter();
mySqlDataAdapter.SelectCommand = mySqlCommand;
DataSet test1DataSet = new DataSet(); //creates data set
mySqlConnection.Open(); // opens connection
Console.WriteLine("Retrieving rows from the test table");
string dataTableName = "myDataTable";
mySqlDataAdapter.Fill(test1DataSet, dataTableName);
DataTable myDataTable = test1DataSet.Tables[myDataTable]; //i get an error here
foreach (DataRow myDataRow in myDataTable.Rows) //iterates over rows in table
{
//Console.WriteLine("Field01") = + myDataRow[("field01")]; // i had to comment out this region because also get an error, but this is not my doubt right now
//Console.WriteLine("Field02") = + myDataRow[("field02")];
//Console.WriteLine("Field03") = + myDataRow[("field03")];
}
mySqlConnection.Close(); //close connection
}
}
}
Here's a simple example which you should follow to correct the mistakes in your approach:
SQL stuff
C# DataAdapter method
Note I dont explicitly open the db connection - the DataAdpater does that for me.
C# DataReader example