2011-03-25 138 views
1

我需要一個簡單的程序來更新MS Access數據庫字段。我跟着online tutorial這很簡單,並有代碼工作。但是當我重新實現它時,它似乎不再起作用。這是我的代碼。如何更新MS Access數據庫

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    OleDbConnection conn; 
    OleDbDataAdapter da; 
    DataSet ds; 
    OleDbCommandBuilder cb; 
    DataRow row; 

    private void Form1_Load(object sender, EventArgs e) 
    { 
     conn = new OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0; Data Source = C:\\test.mdb"); 
     da = new OleDbDataAdapter("select* from user", conn); 
     ds = new DataSet(); 

     conn.Open(); 
     da.Fill(ds, "user"); 
     conn.Close(); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     cb = new OleDbCommandBuilder(da); 
     row = ds.Tables["user"].Rows[0]; 

     row[3] = "hello"; 

     da.Update(ds, "user"); 
    } 
} 

user是我的數據庫的表名。我試圖做的是更新字段行[0](第一行)和列[3](第四列)與字符串hello ..我得到的錯誤是Synatx error in FROM clause。經過一些互聯網閱讀後,我發現user必須放在方括號內。所以我做到了。

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    OleDbConnection conn; 
    OleDbDataAdapter da; 
    DataSet ds; 
    OleDbCommandBuilder cb; 
    DataRow row; 

    private void Form1_Load(object sender, EventArgs e) 
    { 
     conn = new OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0; Data Source = C:\\test.mdb"); 
     da = new OleDbDataAdapter("select* from [user]", conn); 
     ds = new DataSet(); 

     conn.Open(); 
     da.Fill(ds, "user"); 
     conn.Close(); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     cb = new OleDbCommandBuilder(da); 
     row = ds.Tables["user"].Rows[0]; 

     row[3] = "hello"; 

     da.Update(ds, "user"); 
    } 
} 

當我這樣做,我得到一個新的錯誤,Syntax error in UPDATE statement。我做了很多互聯網閱讀,但似乎沒有解決這個問題。他們都以不同的方式使用了Update命令。我只知道這種方式。我的代碼有什麼問題?特別是因爲這之前工作。或者不是這種更新適當技術的方式?請幫助我使用代碼,而不是我不遵循的技術術語。或者其他任何程序在ms訪問中更新?

謝謝。

+2

我刪除了我的答案,因爲在查看代碼時,我看到了一些問題。而不是嘗試重構它,我建議閱讀這篇文章:http://msdn.microsoft.com/en-us/library/tf579hcz(VS.90).aspx – David 2011-03-25 18:04:25

+0

^謝謝大衛。讓我通過.. – nawfal 2011-03-25 18:08:20

回答

4

我從來沒有嘗試過用.NET DataSet前訪問Access數據庫,但我認爲你可以像這樣的東西代替的button1_Click代碼:

private void button1_Click(object sender, EventArgs e) 
{ 
    conn.Open(); 

    string query = "UPDATE [user] SET [columnname] = ? WHERE id = ?"; 
    var accessUpdateCommand = new OleDbCommand(query, conn); 
    accessUpdateCommand.Parameters.AddWithValue("columnname", "hello"); 
    accessUpdateCommand.Parameters.AddWithValue("id", 123); // Replace "123" with the variable where your ID is stored. Maybe row[0] ? 
    da.UpdateCommand = accessUpdateCommand; 
    da.UpdateCommand.ExecuteNonQuery(); 

    conn.Close(); 
} 

是的,我知道你會會失去DataSet的某些好處,但研究表明,常規的OleDbDataAdapter.Update函數在Access中運行不正常。

+0

讓我測試一下。我會在這裏發佈我的回覆:) – nawfal 2011-03-25 18:12:26

+0

當我運行,我得到這個特殊的錯誤消息.'The OleDbParameterCollection只接受非null OleDbParameter類型對象,而不是String對象'在accessUpdateCommand.Parameters.Add(「你好」);線。 可能是什麼原因? – nawfal 2011-03-25 19:21:44

+1

@nawfal - 對不起。嘗試更改accessUpdateCommand.Parameters.Add(「hello」); to accessUpdateCommand.Parameters.AddWithValue(「columnname」,「hello」);並在下一行上做同樣的事情。我會編輯我的答案來反映這一點。 – 2011-03-25 20:04:03

0
student = txtStudent.Text; 
      age = txtAge.Text; 
      address = txtAddress.Text; 
      section = CBSection.Text; 



      string ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\STI.accdb"; 
      string queryString = "UPDATE Details set StudentName='"+student+"',Age='"+age+"',Address='"+address+"' where Section/Course="+section+""; 
      OleDbConnection connection = new OleDbConnection(ConnectionString); 
      OleDbCommand command = new OleDbCommand(); 
      command.CommandType = CommandType.Text; 
      command.CommandText = queryString; 
      command.Connection = connection; 
      connection.Open(); 
      { 
       try 
       { 
        command.ExecuteNonQuery(); 
        MessageBox.Show("StudentName : " + student + "\nAge : " + age + "\nAddress : " + address + "\nSection : " + section + "\nHas been successfully Enrolled"); 
       } 
       catch (Exception ex) 
       { 
        MessageBox.Show(ex.Message); 
       } 
      }*strong text*