【问题标题】:Changing Existing Row and Delete Full Row更改现有行并删除整行
【发布时间】:2011-10-27 21:15:35
【问题描述】:

我尝试创建一个程序,通过单击按钮,程序将从 TextBoxes 中获取全部信息并将其设置在之前的特定行上,并在 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_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));

它的钢不工作。我从最后一条评论中得到了错误。

【问题讨论】:

    标签: c# sql ms-access oledb edit


    【解决方案1】:

    这有一些问题。

    首先,在 SQL 命令中,“值”后面有两个开括号,其中一个没有闭括号。第二个是您实际上并没有从插入语句中的变量中获取值,而是您试图将文本“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.OleDbException (0x80040E10): Can not value for a or more required parameters
    • 如果你想删除整行,我现在编辑的答案的第二部分在你的程序调用时应该可以工作:string myDeleteQuery = "DELETE FROM tblCodons WHERE codonsFullName='" + this。姓名+“'”;我最初忘记包含单引号,这应该也是导致您原始评论错误的原因。您发布的错误表明有一个值丢失或为空,如果值周围没有单引号,肯定会发生这种情况。如果您尝试我的编辑,它应该可以工作。
    • 再次感谢@Ben 的回复,但现在我收到另一个错误:“System.Data.OleDb.OleDbException (0x80040E14):语法错误(缺少运算符)表示查询”。我没有尝试删除 SQL 命令呢
    • 我把所有的“myInsertQuery”放在()中,我现在得到这个错误:System.FormatException:索引(从零开始)必须大于或等于零并且小于参数的大小列表。
    • 您可以发表评论或用您编辑的内容编辑您的原始问题吗?使用我发布的代码,我可以连接到我在 Access 中创建的一个简单表,并根据 codonsFullName 更新行。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-05
    • 1970-01-01
    • 2012-04-13
    • 2018-03-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多