2013-04-26 102 views
0

當我試圖更新使用OleDbConnection MS Access數據庫(.mdb文件),我得到這個錯誤更新MS Access數據庫:在下面的代碼使用OLEDB連接

Microsoft JET Database Engine

at System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr)
at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult)
at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult)
at System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult)
at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method)
at System.Data.OleDb.OleDbCommand.ExecuteNonQuery()
at CostEstimate.Form1.processdbBtn_Click(Object sender, EventArgs e) in C:\Users\sy\Visual Studio 2008\Projects\demo\demo\Form1.cs :line 137

137線是指command.ExecuteNonQuery();

請在下面找到的代碼,並幫我找出這個問題

OleDbConnection conn = new OleDbConnection(); 
    conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" + @"data source=C:\Users\sy\Visual Studio 2008\Projects\demo\demo\CE_Database.mdb;"; 

    conn.Open(); 

    OleDbCommand command = new OleDbCommand(); 
    string[][] allData = [[1,'a'],[2,'b'],[3,'c']] 

    foreach (string[] individualData in allData) 
    { 
    command.CommandText = @"insert into CETable(JobCode,JobName) Values(individualData[0],individualData[1])"; 
    command.Connection = conn; 
    command.ExecuteNonQuery(); 
    } 

    conn.Close(); 
+0

JET是否支持傳入的OLE-DB? – 2013-04-26 04:09:22

回答

0

試試這個:

OleDbConnection conn = new OleDbConnection(); 
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" + @"data source=C:\Users\sy\Visual Studio 2008\Projects\demo\demo\CE_Database.mdb;"; 
conn.Open(); 
string[][] allData = [[1,'a'],[2,'b'],[3,'c']] 

foreach (string[] individualData in allData) 
{ 
    OleDbCommand command = new OleDbCommand() 
    { 
     Connection = conn, 
     CommandText = string.Format(@"insert into CETable(JobCode,JobName) Values({0}, {1});", individualData[0], individualData[1])"; 
    }; 
    command.ExecuteNonQuery(); 
} 
conn.Close(); 

但我還建議您通過參數設置每個字段值。這是一個更好的做法,以確保沒有注射

+0

遇到同樣的錯誤 – Coolenough 2013-04-26 04:28:16