C# 更改现有行并删除整行

C# 更改现有行并删除整行,c#,sql,ms-access,oledb,edit,C#,Sql,Ms Access,Oledb,Edit,我试着制作一个程序,通过点击一个按钮,程序将从文本框中获取全部信息,并将其设置在以前的特定行上,然后在Access数据库中更改它 我通过单击任何DataGridView单元格中的一个内容完成了动态数据绑定,它将整行的信息放入合适的文本框中。我以类似于合适列名的方式定义了文本框的名称 当我调试它时,程序停止并告诉我SQL命令有问题 我还需要如何删除整行和更新数据库的代码,我不知道该怎么做 以下是我编写的代码: namespace myProject { public partial class

我试着制作一个程序,通过点击一个按钮,程序将从文本框中获取全部信息,并将其设置在以前的特定行上,然后在Access数据库中更改它

我通过单击任何DataGridView单元格中的一个内容完成了动态数据绑定,它将整行的信息放入合适的文本框中。我以类似于合适列名的方式定义了文本框的名称

当我调试它时,程序停止并告诉我SQL命令有问题

我还需要如何删除整行和更新数据库的代码,我不知道该怎么做

以下是我编写的代码:

namespace myProject
{
  public partial class EditCodons : Form
  {
    private OleDbConnection dataConnection;
    private int row;
    private string name;
    public EditCodons()
    {
      InitializeComponent();
      OpenDb();
    }

    private void EditCodons_Load(object sender, EventArgs e)
    {
      // TODO: This line of code loads data into the 'myProjectDataSet.tblCodons' table. You can move, or remove it, as needed.
      this.tblCodonsTableAdapter.Fill(this.myProjectDataSet.tblCodons);
    }

    private void OpenDb()
    {
      dataConnection = new OleDbConnection();
      try
      {
        dataConnection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Projects_2012\\Project_Noam\\Access\\myProject.accdb";
        dataConnection.Open();
      }
      catch (Exception e)
      {
        MessageBox.Show("Error accessing the database: " +
                         e.Message,
                         "Errors",
                         MessageBoxButtons.OK,
                         MessageBoxIcon.Error);
      }
    }

    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
      this.row = e.RowIndex;
      this.name = fullName.Text = dataGridView1.Rows[e.RowIndex].Cells[8].FormattedValue.ToString();
      codon1.Text = dataGridView1.Rows[e.RowIndex].Cells[0].FormattedValue.ToString();
      codon3.Text = dataGridView1.Rows[e.RowIndex].Cells[1].FormattedValue.ToString();
      triplet1.Text = dataGridView1.Rows[e.RowIndex].Cells[2].FormattedValue.ToString();
      triplet2.Text = dataGridView1.Rows[e.RowIndex].Cells[3].FormattedValue.ToString();
      triplet3.Text = dataGridView1.Rows[e.RowIndex].Cells[4].FormattedValue.ToString();
      triplet4.Text = dataGridView1.Rows[e.RowIndex].Cells[5].FormattedValue.ToString();
      triplet5.Text = dataGridView1.Rows[e.RowIndex].Cells[6].FormattedValue.ToString();
      triplet6.Text = dataGridView1.Rows[e.RowIndex].Cells[7].FormattedValue.ToString();
      fullName.Text = dataGridView1.Rows[e.RowIndex].Cells[8].FormattedValue.ToString();
      start.Text = dataGridView1.Rows[e.RowIndex].Cells[9].FormattedValue.ToString();
      end.Text = dataGridView1.Rows[e.RowIndex].Cells[10].FormattedValue.ToString();
    }

    private void addRow_Click(object sender, EventArgs e)
    {
    }

    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 = "INSERT INTO tblCodons (codonsCodon1, codonsCodon3, codonsTriplet1, codonsTriplet2, codonsTriplet3,codonsTriplet4, codonsTriplet5, codonsTriplet6, codonsFullName, codonsStart, codonsEnd  )" +
            " Values(('codon1.Text', 'codon3.Text', 'triplet1.Text', 'triplet2.Text', 'triplet3.Text','triplet4.Text', 'triplet5.Text', 'triplet6.Text', 'fullName.Text', 'start.Text', 'end.Text')" +
            "WHERE codonsFullName =" + this.name;
      OleDbCommand myCommand = new OleDbCommand(myInsertQuery);
      myCommand.Connection = myConnection;
      myConnection.Open();
      myCommand.ExecuteNonQuery();
      myCommand.Connection.Close();
    }

    private void reset_Click(object sender, EventArgs e)
    {
      codon1.Clear();
      codon3.Clear();
      triplet1.Clear();
      triplet2.Clear();
      triplet3.Clear();
      triplet4.Clear();
      triplet5.Clear();
      triplet6.Clear();
      start.Clear();
      end.Clear();
      fullName.Clear();
    }

    private void deleteRow_Click(object sender, EventArgs e)
    {

    }

  }
}
*编辑:*我尝试更改SQL,错误列表已写入

错误1只有赋值、调用、递增、递减和新对象表达式可以用作语句C:\Projects\u 2012\Project\u Noam\Project\myProject\myProject\EditCodons.cs 82 59 myProject 错误2无效的表达式术语C:\Projects\u 2012\Project\u Noam\Project\myProject\myProject\EditCodons.cs 82 60 myProject 错误3;预期C:\Projects\u 2012\Project\u Noam\Project\myProject\myProject\EditCodons.cs 82 60 myProject 所以我把它改成:

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}', codonsStart='{9}', " + 
        "codonsEnd='{10}' WHERE codonsFullName='{11}'",
        triplet3.Text, triplet4.Text, triplet5.Text, triplet6.Text,
        fullName.Text, start.Text, end.Text, this.name));

而且它不起作用。我从最后的评论中得到了错误。

这有一些地方不对劲

首先,在SQL命令中,值后面有两个开括号,其中一个不是闭合的。第二个问题是,实际上您并没有从insert语句中的变量中获取值,而是尝试将文本codon1.text、codon3.text等插入到数据库中。根据列数据类型,这可能会导致错误

但是,主要问题是不能使用Insert语句进行更新。相反,您需要使用Update语句,如下所示

UPDATE tableName SET column=value WHERE column=something
设置SQL语句的代码行应为:

//Linebreaks used to make the code easier to read.
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}', codonsStart='{9}', " + 
            "codonsEnd='{10}' WHERE codonsFullName='{11}'",
            codon1.Text, codon3.Text, triplet1.Text, triplet2.Text,
            triplet3.Text, triplet4.Text, triplet5.Text, triplet6.Text,
            fullName.Text, start.Text, end.Text, this.name);
此语句将更新codonsFullName等于This.name变量中存储内容的任何行

删除更容易,可通过以下方式完成:

string myDeleteQuery = "DELETE FROM tblCodons WHERE codonsFullName='" + this.name + "'";
假设codonsFullName是您要删除的内容

编辑:


我编辑了我的答案,以包含我最初忘记包含的单引号。

当我尝试单击updaye按钮时,错误消息显示如下:System.Data.OleDb.OLEDBEException 0x80040E10:无法为一个或多个所需参数设置值如果试图删除整行,我现在编辑的答案的第二部分在从程序调用时应该可以工作:string myDeleteQuery=DELETE from tblCodons,其中codonsFullName='+this.name+';我最初忘记了包含单引号,这也应该是导致您原始评论错误的原因。您发布的错误表明有一个值丢失或为空,如果该值周围没有单引号,则肯定会发生这种情况。如果你尝试我的编辑,它应该可以工作。再次感谢@Ben的响应,但现在我收到另一个错误:System.Data.OleDb.OledBeException 0x80040E14:语法错误缺少表示查询的运算符。我不尝试删除SQL顺序yetI将所有myInsertQuery放入,我现在收到此错误:System.FormatException:基于零的索引必须大于或等于零并且小于参数列表的大小。您可以发布评论或使用您编辑的内容编辑原始问题吗?使用我发布的代码,我能够连接到Access中的一个简单表,并基于codonsFullName更新行。