2014-09-04 5 views
0

의 C#을 사용하여 홈에 데이터를 삽입 할 수는 없지만 데이터를 삽입 할 수 없지만 오류는내가 여러 번 시도

public static void SaveCorresDetails(string cN, string cId, string pId, string rI) 
    { 
     OleDbConnection myConnection = GetConnection(); 
     string myQuery = "INSERT INTO RAHANICorrespondent(DateIssue, Action, Recipient, Remarks) VALUES ('" + cN + "' , '" + cId + "', '" + pId + "','" + rI + "')"; 
     OleDbCommand myCommand = new OleDbCommand(myQuery, myConnection); 

     try 
     { 
      myConnection.Open(); 
      myCommand.ExecuteNonQuery(); 
     } 
     catch (Exception ex) 
     { 
      Console.WriteLine("Exception in DBHandler", ex); 
     } 
     finally 
     { 
      myConnection.Close(); 
     } 

    } 

도와주세요이 데이터를 추가 할 수있는 클래스 파일 될 것

protected void Button1_Click(object sender, EventArgs e) 
    { 
     string n = TextBox1.Text.Trim(); 
     string c = TextBox2.Text.Trim(); 

     string p = TextBox3.Text.Trim(); 
     string r = TextBox4.Text.Trim(); 


     // save the data into the database 
     Data.SaveCorresDetails(n, c, p, r); 
     Label6.Text = "Package amount of " + n + " was saved"; 
     TextBox1.Text = ""; 
     TextBox2.Text = ""; 
     TextBox3.Text = ""; 
     TextBox4.Text = ""; 
    } 

감사합니다 ...

+0

ExecuteNonQuery는 무엇을 반환합니까? –

+0

execute non query를 사용하여 연결에 대한 Transact-SQL 문을 실행하고 영향을받은 행 수를 반환합니다. –

+0

아니면 -1이 반환됩니까? 또한 콘솔 앱입니까? –

답변

0

대신 db 매개 변수를 사용해보십시오. 다음

OleDbConnection connection = new OleDbConnection(""); 
string query = "INSERT INTO myTable (a,b,c) Values(@a, @b @c)"; 
OleDbParameter param = new OleDbParameter("a", "Hi man"); 
param.DbType = ... 
OleDbCommand command = new OleDbCommand(query); 
command.CommandType = System.Data.CommandType.Text; 
int result = command.ExecuteNonQuery(); 

그리고

은 점점 오류가 있는지 확인이 도움이 될 경우 그 볼 로그인합니다.