代码如下。在我的代码中,我更新了现有行(从现有表),程序从文本框获取所有更新的值。表 autoNumber 字段的末尾(最后一列)名为“codonsAutoNum”
我不知道如何从数据库中获取值(自动编号值)。我需要有'??'的位置(在代码中)的值。
谢谢大家的帮助!
编码:
private void update_Click(object sender, EventArgs e)
{
string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Projects_2012\\Project_Noam\\Access\\myProject.accdb";
OleDbConnection myConnection = new OleDbConnection(connectionString);
string myInsertQuery =(String.Format("UPDATE tblCodons SET codonsCodon1='{0}', codonsCodon3='{1}', " +
"codonsTriplet1='{2}', codonsTriplet2='{3}', codonsTriplet3='{4}', codonsTriplet4='{5}', " +
"codonsTriplet5='{6}', codonsTriplet6='{7}', codonsFullName='{8}'" +
" WHERE codonsAutoNum='{9}'",codon1.Text, codon3.Text ,triplet1.Text, triplet2.Text,
triplet3.Text, triplet4.Text, triplet5.Text, triplet6.Text,
fullName.Text,??));
OleDbCommand myCommand = new OleDbCommand(myInsertQuery);
myCommand.Connection = myConnection;
myConnection.Open();
myCommand.ExecuteNonQuery();
myCommand.Connection.Close();
编辑:它真的很困惑。我的代码应该是如何出现的?我需要将“SELECT @@identity”放在哪里?我不明白为什么我不能只取值?(我需要它作为所选行的索引来确定更新在哪里。当我识别这个(下)时它运行良好,但它不允许我更改全名字段..有比自动编号列更好的解决方案?
private void update_Click(object sender, EventArgs e)
{
string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Projects_2012\\Project_Noam\\Access\\myProject.accdb";
OleDbConnection myConnection = new OleDbConnection(connectionString);
string myInsertQuery =(String.Format("UPDATE tblCodons SET codonsCodon1='{0}', codonsCodon3='{1}', " +
"codonsTriplet1='{2}', codonsTriplet2='{3}', codonsTriplet3='{4}', codonsTriplet4='{5}', " +
"codonsTriplet5='{6}', codonsTriplet6='{7}', codonsFullName='{8}'" +
" WHERE codonsFullName = {9}",codon1.Text, codon3.Text ,triplet1.Text, triplet2.Text,
triplet3.Text, triplet4.Text, triplet5.Text, triplet6.Text,
fullName.Text,this.name));
OleDbCommand myCommand = new OleDbCommand(myInsertQuery);
myCommand.Connection = myConnection;
myConnection.Open();
myCommand.ExecuteNonQuery();
myCommand.Connection.Close();
this.tblCodonsTableAdapter.Fill(this.myProjectDataSet.tblCodons);
name=全名的保存值,在每次点击时值变化
还有一个问题。我尝试对 e.Row 做同样的事情(我在每次点击表格时保存),我得到一个错误“System.Data.OleDb.OleDbException (0x80004005): can cue a duplicate values on the index, or on the mainKey 或在关系中”</p>