0

有人能告诉我如何解决这个错误吗?

 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();
    }
}
4

2 回答 2

0

您正在使用 SqlCommand/etc,它需要使用 SqlConnection 对象而不是 OleDbConnection。

它是您要连接的 SQL 数据库吗?如果是这样,请SqlConnection改用

编辑:显然不是,读取连接字符串...:D

于 2012-10-15T15:52:26.063 回答
0

您正在混淆 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();
    }
}
于 2012-10-15T15:51:37.303 回答