2012-10-15 85 views
0

有人能告訴我如何解決這個錯誤?從Access數據庫讀取數據到列表框

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

更改SqlCommand時使用的OleDbCommand類。 –

+0

OleCommand和OleDataReader去與OleDbConnection –

回答

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

您正在使用需要使用o的SqlCommand/etc f SqlConnection對象而不是OleDbConnection。

它是連接到的SQL數據庫嗎?如果是使用SqlConnection代替

編輯:顯然不是,讀取連接字符串...:d