2011-10-27 3 views
0

버튼을 클릭하면 프로그램이 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 \ 프로젝트 그래서 난이에 changet \ 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)); 

과 강이 작동하지 않습니다. 마지막 코멘트에서 오류가 발생합니다.

답변

0

몇 가지 문제가 있습니다.

먼저 SQL 명령에서 "값"다음에 두 개의 열린 괄호가 있으며 그 중 하나는 닫히지 않습니다. 두 번째는 실제로 insert 문에서 변수의 값을 가져 오지 않고 "codon1.Text", "codon3.Text"등의 텍스트를 데이터베이스에 삽입하려고한다는 것입니다. 열 데이터 유형에 따라 오류가 발생할 수 있습니다.

그러나이 주된 문제점은 Insert 문으로 업데이트 할 수 없다는 것입니다. 오히려, 당신이

UPDATE tableName SET column=value WHERE column=something 

처럼 보이는 UPDATE 문을 사용하는 데 필요한 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

전체 행을 삭제하려고하면 , 내 편집 된 답변의 두 번째 부분은 프로그램에서 호출 할 때 작동합니다. 문자열 myDeleteQuery = "DELETE FROM tblCodons WHERE codonsFullName = '"+ this.name + "'" "; 원래 원래 주석에서 오류의 원인이되는 작은 따옴표를 포함하는 것을 잊어 버렸습니다. 귀하가 게시 한 오류는 누락되었거나 비어있는 값이 있음을 나타내며 값 주변에 작은 따옴표가 없으면 분명히 발생합니다. 편집을 시도하면 효과가 있습니다. – Ben

+0

응답을 다시 '@Ben 감사하지만 지금은 또 다른 오류가 발생합니다 : "System.Data.OleDb.OleDbException (0x80040E14) : 쿼리 오류 표현 (누락 된 연산자)". SQL 순서를 아직 삭제하려고 시도하지 않았습니다. – user1017315