2011-03-25 144 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*