C# 将Access数据库中的数据读取到列表框中
有人能告诉我如何修复这个错误吗C# 将Access数据库中的数据读取到列表框中,c#,.net,ado.net,datareader,C#,.net,Ado.net,Datareader,有人能告诉我如何修复这个错误吗 SqlCommand cmd = new SqlCommand(sqlCmd, conn) --> conn: Aurgument type 'System.Data.OleDb.OleDbConnection' is not assignable to parameter type 'System.Data.SqlClient.SqlConnection'. private void Form1_Load(object sender, Event
SqlCommand cmd = new SqlCommand(sqlCmd, conn)
--> conn: Aurgument type 'System.Data.OleDb.OleDbConnection' is not assignable to parameter type 'System.Data.SqlClient.SqlConnection'.
private void Form1_Load(object sender, EventArgs e)
{
string connString = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=C:\\Users\\KevinDW\\Desktop\\dotNET\\Week 5\\Prak1\\demo1.accdb";
OleDbConnection conn = new OleDbConnection(connString);
conn.Open();
string sqlCmd = "SELECT CursusNaam FROM tblCursus";
SqlCommand cmd = new SqlCommand(sqlCmd, conn);
using (SqlDataReader reader = cmd.ExecuteReader())
{
listBox1.Items.Add(reader);
}
conn.Close();
}
}
你把Sql和OleDb混在一起了 使用
OleDbCommand
而不是SqlCommand
并使用OleDBDataReader
而不是SqlDataReader
例如:
private void Form1_Load(object sender, EventArgs e)
{
string connString = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=C:\\Users\\KevinDW\\Desktop\\dotNET\\Week 5\\Prak1\\demo1.accdb";
OleDbConnection conn = new OleDbConnection(connString);
conn.Open();
string sqlCmd = "SELECT CursusNaam FROM tblCursus";
OleDbCommand cmd = new OleDbCommand(sqlCmd, conn);
using (OleDBDataReader reader = cmd.ExecuteReader())
{
listBox1.Items.Add(reader);
}
conn.Close();
}
}
您使用的是SqlCommand/etc,它需要使用SqlConnection对象而不是OleDbConnection 您要连接的是SQL数据库吗?如果是这样,请改用
SqlConnection
编辑:显然不是,读取连接字符串…:D使用OleDbCommand class.OleCommand和OLEDATReader更改SqlCommand以使用OleDbConnection