2011-10-27 44 views
0

我試圖通過單擊按鈕來創建一個過程,該程序將從TextBoxes中獲取所有信息並將其設置在之前的特定行上,然後在Access數據庫中對其進行更改。更改現有行並刪除整行

我做了動態數據綁定,通過單擊任何DataGridView單元格中的一個內容,它可以完美地工作,它將整行的信息放入合適的TextBoxes中。我以類似於合適的列名稱的方式定義了文本框的名稱。

當我調試它時,程序停止並告訴我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 myProject 錯誤2無效的表達式項')'C:\ Projects_2012 \ Project_Noam \ Project \ myProject \ myProject \ EditCodons.cs 82 60 myProject 錯誤3;預計C:\ Projects_2012 \ Project_Noam \項目\ myProject的\ myProject的\ EditCodons.cs 82 60 myProject的 ,所以我changet它這樣的:

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)); 

而且鋼不工作。我從最後的評論中得到錯誤。

回答

0

這有一些問題。

首先,在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是你想刪除的東西。

編輯:

我編輯我的回答包括單引號,我忘了最初包括。

+0

當我嘗試單擊updaye按鈕錯誤消息顯示此:System.Data.OleDb.OleDbException(0x80040E10):無法爲一個或多個所需的參數值 – user1017315

+0

如果您要刪除整行,我現在編輯的答案的第二部分應該從你的程序調用時工作: string myDeleteQuery =「DELETE FROM tblCodons WHERE codonsFullName ='」+ this.name +「'」; 我最初忘記了包括單引號,它應該也是你原來的評論引起錯誤的原因。您發佈的錯誤表示存在缺失或空的值,如果值周圍沒有單引號,則肯定會發生該值。如果你嘗試我的編輯,它應該工作。 – Ben

+0

再次感謝'@Ben爲您的響應,但現在我得到另一個錯誤:「System.Data.OleDb.OleDbException(0x80040E14):語法錯誤(缺少運算符)表示查詢」.Ididn't嘗試刪除SQL順序,但 – user1017315