Hi,
Trying to use c# to connect to a mysql database that I have set up on a server somewhere, but I am currently getting the following error:
Error 11 The type 'System.Data.Common.DbConnection' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. C:\Documents and Settings\mb21\My Documents\Visual Studio 2008\Projects\Database3\Database3\Form1.cs 17 25 Database3
The code I am using is the following:
Code:
using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using MySql.Data;
using MySql.Data.MySqlClient;
namespace Database3
{
public partial class Form1 : Form
{
MySqlConnection conn = null;
MySqlDataReader dr;
MySqlCommand cmd = null;
public Form1()
{
InitializeComponent();
}
private void DisplayErrors(MySqlException errSQLException)
{
for (int i = 0; i < errSQLException.Errors.Count; i++)
{
MessageBox.Show("Index # " + i + "\n" + "Error:" +
errSQLException.Errors[i].ToString() + "\n");
}
}
private void btnConnect_Click_1(object sender, EventArgs e)
{
string strSQL = "Select * from users";
try
{
cmd = new MySqlCommand(strSQL, conn);
dr = cmd.ExecuteReader(CommandBehavior.SequentialAccess);
while (dr.Read())
{
txtData.Text = dr.GetString(1);
}
}
catch (MySqlException ex)
{
DisplayErrors(ex);
}
finally
{
dr.Close();
conn.Close();
}
}
private void Form1_Load_1(object sender, EventArgs e)
{
string strConnection = "SERVER=server;" + "DATABASE=database;" + "UID=username;" + "PASSWORD=password;";
//"Data Source = .\\SQLEXPRESS;" +
//"Initial Catalog=SampleDatabase.mdf;";// +
//"Integrated Security=SSPI;" +
// "User ID=mb21;" +
//"Password=mscim2010;";
txtData.Text = "blah";//This line does run
try
{
conn = new MySqlConnection(strConnection);
txtData.Text = txtData.Text + "anything";//This line does run
conn.Open();
}
catch (MySqlException ex)
{
DisplayErrors(ex);
conn.Close();
}
}
}
}
There are several errors, all like the one on the top, which correspond to the following lines:
Code:
MySqlConnection conn = null;
MySqlDataReader dr;
MySqlCommand cmd = null;
private void DisplayErrors(MySqlException errSQLException)
I installed mysql connector.net version 6.1.3 and I am using a Visual Studio 2008 for developing the code.
I am new to C#, so would be grateful for any help. It seems the problem is lies with the declaration of the variables (conn, dr, cmd and errSQLException) but I can't see what the problem is. Any and all help is appreciated.
Brownie