몇 가지가 있습니다. 당신이 한 일에 대한 당신의 프리젠 테이션에 문제가 있습니다. 매개 변수는 문자열 문자열이 아니며 SqlParamaters이며 SqlParameter p1 = new SqlParameter("@col1", "val1");
으로 직접 정의되거나 명령 개체에 직접 추가 될 수 있습니다 cmd.Parameters.AddWithValue("@col1", "val1");
쿼리의 실제 실행은 실제로 수행 한 것과 같습니다. command.executeNonQuery()
메서드의 한 가지 공통점은 영향을받는 행의 양을 반영하는 정수를 반환한다는 것입니다. 삽입의 경우 1이어야하지만 여러 레코드를 삭제하는 경우 더 높거나 제한적일 수 있습니다 (WHERE
통해). 0이 될 수 있습니다. 쉽게 문제를 인식하기 위해 catch
문 내에 음수를 할당합니다.
int RowsAffected;
using (SqlConnection conn = new SqlConnection(strConn)) {
string cmdText = "insert into xxx(col1, col2) values(@col1,@col2)";
using (SqlCommand cmd = new SqlCommand(cmdText, conn)) {
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@col1", "val1");
cmd.Parameters.AddWithValue("@col2", "val2");
try {
conn.Open();
RowsAffected = cmd.ExecuteNonQuery();
}
catch (SqlException sx) {
RowsAffected = -1;
Console.Write(sx); // your SQL error handling
}
catch (Exception ex) {
RowsAffected = -2;
Console.Write(ex); // other exception handling
}
finally {
// your cleanup routines
conn.Close();
Console.Write("Rows Added = " + RowsAffected);
}
}
}
포스트 코드 :
어쨌든, 이것은 당신이 ADO를 통해 SQL 끊다에 실행할 명령문을 작성하는 방법의 대략적인 레이아웃입니다. – Gusman
"command.executeNonQuery()"를 사용해 보셨나요? ??? 이 질문은 모든 중요한 중요한 질문이 부족합니다. 주로 다른 사람들이 도움을받을 수있는 세부 사항이 필요합니다. –