WCF 서비스를 통해 컴팩트 SQL 데이터베이스 서버에 연결하고 있으며 Command.ExecuteNonQuery()
. 나는 이것을 고치려고 노력했지만 무엇이 잘못되었는지 모르겠습니다.
예외 :
System.Data.SqlServerCe.dll에서 'System.Data.SqlServerCe.SqlCeException'유형의 예외가 발생했지만 사용자 코드에서 처리되지 않았습니다.
코드:
//The connectionString can be found in the properties table of the database
string connString = "Data Source=C:\\Users\\User\\documents\\visual studio 2012\\Projects\\ADO_LINQ\\ADO_LINQ\\App_Data\\MyDatabase.sdf;Persist Security Info = False";
SqlCeConnection myConnection = new SqlCeConnection(connString);
myConnection.Open();
// Create the query
string myQuery = "INSERT INTO Player " +
" VALUES (" + registrationID + "," +
firstName + ", " +
lastName + ", " +
phoneNumber + ", " +
address + ", " +
dateOfBirth + ");";
//Initialuze the command
SqlCeCommand myCommand = new SqlCeCommand(myQuery, myConnection);
//Run the command
myCommand.ExecuteNonQuery();
//Close the connection
myConnection.Close();
문자열 데이터 유형 주위에 작은 따옴표가 없습니다. registrationID 만 Integer 데이터 유형이고 다른 모든 열이 String 데이터 유형이라고 가정하면 쿼리는 다음과 같아야합니다 ...
// Create the query
String myQuery = "INSERT INTO Player " +
" VALUES (" + registrationID + ", '"+ firstName +"' , '"+lastName+"' , '"+phoneNumber+ "', '"+ address +"', '"+dateOfBirth+"' );";
더 좋고 안전한 옵션은 Parametrised 쿼리를 사용하는 것입니다. 이 같은.....
String connString = @"Data Source=C:\Users\User\documents\visual studio 2012\Projects\ADO_LINQ\ADO_LINQ\App_Data\MyDatabase.sdf;Persist Security Info = False";
using(SqlCeConnection myConnection = new SqlCeConnection(connString))
{
// Create the query
String myQuery = "INSERT INTO Player " +
" VALUES (@registrationID , @firstName , @lastName , @phoneNumber, @address , @dateOfBirth );";
//Initialuze the command
SqlCeCommand myCommand = new SqlCeCommand(myQuery, myConnection);
// Add parameters
myCommand.Parameters.AddWithValue("@registrationID" ,registrationID);
myCommand.Parameters.AddWithValue("@firstName" , firstName);
myCommand.Parameters.AddWithValue("@lastName" , lastName);
myCommand.Parameters.AddWithValue("@phoneNumber" , phoneNumber);
myCommand.Parameters.AddWithValue("@address" , address);
myCommand.Parameters.AddWithValue("@dateOfBirth" , dateOfBirth);
//Open Connection
myConnection.Open();
//Run the command
myCommand.ExecuteNonQuery();
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다