C# WCF服务不断返回false
因此,我在C中创建并调用了一个WCF服务,但不断收到一个错误的回复,我不确定这是为什么。这可能与连接字符串有关,但将其从当前字符串更改只会给我带来错误 这是我的密码:C# WCF服务不断返回false,c#,sql,database,wcf,ado.net,C#,Sql,Database,Wcf,Ado.net,因此,我在C中创建并调用了一个WCF服务,但不断收到一个错误的回复,我不确定这是为什么。这可能与连接字符串有关,但将其从当前字符串更改只会给我带来错误 这是我的密码: //Create the new connection SqlConnection myConnection = new SqlConnection(); //Create the query String myQuery = "INSERT INTO Player (registrationID, firstNa
//Create the new connection
SqlConnection myConnection = new SqlConnection();
//Create the query
String myQuery = "INSERT INTO Player (registrationID, firstName, lastName, phoneNumber, Address, dateOfBirth) " +
" VALUES ('" + registrationID + "', '" + firstName + "', '" + lastName + "', '" + phoneNumber + "', '" + Address + "', '" + dateOfBirth + "');";
//The connectionString can be found in the properties table of the database
myConnection.ConnectionString = "Data Source=C:/Users/User/Documents/Visual Studio 2012/Projects/ADO_LINQ/ADO_LINQ/App_Data/MyDatabase.sdf";
//Initialuze the command
SqlCommand myCommand = new SqlCommand(myQuery, myConnection);
SqlDataReader myReader;
//Run the command
try
{
myConnection.Open();
myReader = myCommand.ExecuteReader();
//Return true if it was successful
return true;
}
catch (Exception ex)
{
return false;
}
正如Soner指出的,您的代码容易受到SQL注入攻击,这可以通过使用参数化查询进行补救。此外,最佳实践是将using块与连接一起使用,以便在using块中的代码退出后正确关闭和处置它。当前,您甚至在完成连接时都没有关闭连接 此外,ExecuteOnQuery对此已经足够了—它将运行该命令,然后返回受影响的行数,在本例中应为1。除了在异常情况下使用catch块外,还可以检查受影响的行数并使用该行数确定成功/失败。实际上,除非在执行命令时抛出异常,否则我不会期望值1以外的任何值 最后,您发布的代码正在吞噬异常。你应该做一些异常记录,执行一些其他代码,重新播放它-这取决于你的应用程序的要求,而不是简单地返回false 总而言之:
using (SqlConnection myConnection = new SqlConnection())
{
// Create the query
String myQuery = "INSERT INTO Player (registrationID, firstName, lastName, phoneNumber, Address, dateOfBirth) " +
" VALUES (@RegistrationID, @FirstName, @LastName, @PhoneNumber, @Address, @DateOfBirth)";
//The connectionString can be found in the properties table of the database
myConnection.ConnectionString = "Data Source=C:/Users/User/Documents/Visual Studio 2012/Projects/ADO_LINQ/ADO_LINQ/App_Data/MyDatabase.sdf";
//Initialuze the command
SqlCommand myCommand = new SqlCommand(myQuery, myConnection);
myCommand.CommandType = CommandType.Text;
// Here you add the values for the parameters in the query
myCommand.Parameters.Add("@RegistrationID", SqlDbType.VarChar).Value = registrationID;
myCommand.Parameters.Add("@FirstName", SqlDbType.VarChar).Value = firstName;
myCommand.Parameters.Add("@LastName", SqlDbType.VarChar).Value = lastName;
myCommand.Parameters.Add("@PhoneNumber", SqlDbType.VarChar).Value = phoneNumber;
myCommand.Parameters.Add("@Address", SqlDbType.VarChar).Value = address;
myCommand.Parameters.Add("@DateOfBirth", SqlDbType.VarChar).Value = dateOfBirth;
//Run the command
try
{
myConnection.Open();
int rowsAffected = myCommand.ExecuteNonQuery();
if (rowsAffected == 1)
{
return true;
}
else
{
return false;
}
}
catch (Exception ex)
{
// Do something with the exception, like logging it so you can review the error
return false;
}
}
上面的代码将调用包装在using语句中。创建命令时,参数将添加到SqlCommand.parameters集合,然后返回ExecuteOnQuery。如果结果为1,则返回true,否则返回false。如果遇到错误,将返回false,但您还是应该处理异常,以便在需要时进行故障排除。您是否尝试过删除catch块,以便服务崩溃并告诉您出了什么问题?错误是什么?你应该经常使用。这种类型的字符串连接对攻击是开放的。而且使用ExecuteReader没有任何意义,因为您使用的是INSERT语句。因为您的查询不返回任何数据,所以它只是插入数据。只需使用ExecuteOnQuery即可。还可以使用using语句来处理数据库连接和对象。如果您不处理异常(例如,记录异常或其他操作),则捕获异常没有什么意义。当您遇到异常时,简单地返回false意味着您正在吞咽该异常,这将使故障排除成为一件痛苦的事情。谢谢您,这真是一份帮助!你让步了,希望其他人会觉得这很有用!又是thnx。还有一件事,我似乎在连接字符串方面遇到了问题,我从属性中取了一个,但似乎不起作用。有什么建议?@Cornelis-什么麻烦?你有例外吗?您使用的是什么数据库MS SQL、Access、MySQL等?您也可以检查-它们具有几乎所有.SQL的连接字符串,但是使用visual studio中的内置字符串。正在获取异常。异常消息是什么?