更改现有行并删除整行

时间:2011-10-27 21:15:36

标签: c# sql ms-access oledb edit

我尝试创建一个过程,通过单击按钮,程序将从TextBox中获取整个信息并将其设置在之前的特定行上,并在Access数据库中进行更改。

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

当我调试它时,程序停止并告诉我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_2012 \ Project_Noam \ Project \ myProject \ myProject \ EditCodons.cs 82 59 myProject 错误2无效的表达式术语')'C:\ Projects_2012 \ Project_Noam \ Project \ myProject \ myProject \ EditCodons.cs 82 60 myProject 错误3;预期C:\ Projects_2012 \ Project_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));

它钢不起作用。我从上次评论中得到错误。

1 个答案:

答案 0 :(得分:0)

这有一些问题。

首先,在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是您想要删除的内容。

编辑:

我编辑了我的答案,包括我原本忘记包含的单引号。