更新代码用于 SQL 服务器中的存储过程,可能不适用于访问数据库:
string _Update_Emp = "UPDATE AlbahraniNetwork SET FirstName=@FirstName,SecondName=@SecondName,LastName=@LastName,Phone=@Phone,Cell=@Cell,Email=@Email,Address=@Address where FirstName=@FirstName";
string appPath = Path.GetDirectoryName(Application.ExecutablePath);
if (!appPath.EndsWith("\\"))
appPath += "\\";
_Conn.ConnectionString = (@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + appPath + "Database31.accdb");
//_Conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Abdullah\documents\visual studio 2010\Projects\AlbahraniNetwork2\AlbahraniNetwork2\Database31.accdb";
_Conn.Open();
OleDbCommand _Update_Command = new OleDbCommand(_Update_Emp, _Conn);
_Update_Command.Parameters.AddWithValue("@FirstName", FirstName.Text);
_Update_Command.Parameters.AddWithValue("SecondName", SecondName.Text);
_Update_Command.Parameters.AddWithValue("@LastName", LastName.Text);
_Update_Command.Parameters.AddWithValue("@Phone", Phone.Text);
_Update_Command.Parameters.AddWithValue("@Cell", Cell.Text);
_Update_Command.Parameters.AddWithValue("@Email", Email.Text);
_Update_Command.Parameters.AddWithValue("@Address", Address.Text);
_Update_Command.ExecuteNonQuery();
让它简单,而不是使用:
string _Update_Emp = "UPDATE AlbahraniNetwork SET " +
FirstName=\"" + FirstName.Text + "\"" +
",SecondName=\"" + SecondName.Text + "\"" +
",LastName=\""+ LastName.Text + "\"" +
",Phone=\""+ Phone.Text + "\"" +
",Cell=\"" + Cell.Text + "\"" +
",Email=\"" + Email.Text + "\"" +
",Address=\"" + Address.Text + "\"" +
" where FirstName=\"" + FirstName.Text +";";
string appPath = Path.GetDirectoryName(Application.ExecutablePath);
if (!appPath.EndsWith("\\"))
appPath += "\\";
_Conn.ConnectionString = (@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + appPath + "Database31.accdb");
//_Conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Abdullah\documents\visual studio 2010\Projects\AlbahraniNetwork2\AlbahraniNetwork2\Database31.accdb";
_Conn.Open();
OleDbCommand _Update_Command = new OleDbCommand(_Update_Emp, _Conn);