C# WebMethod未关闭SqlConnection?
web服务的另一个问题。这次我用WebMethod传递了两个字符串,并试图将它们保存到数据库中。到现在为止,一直都还不错。一切正常。 但是 但当我关闭应用程序并希望显示来自该数据库/表的数据时,错误消息总是说数据库仍被其他应用程序使用。 以下是我的Web服务代码:C# WebMethod未关闭SqlConnection?,c#,database,webmethod,C#,Database,Webmethod,web服务的另一个问题。这次我用WebMethod传递了两个字符串,并试图将它们保存到数据库中。到现在为止,一直都还不错。一切正常。 但是 但当我关闭应用程序并希望显示来自该数据库/表的数据时,错误消息总是说数据库仍被其他应用程序使用。 以下是我的Web服务代码: [WebMethod] public string GetValues(string value, string value2) { try {
[WebMethod]
public string GetValues(string value, string value2)
{
try
{
string crime = value;
string invest = value2;
SqlConnection cs = new SqlConnection();
cs.ConnectionString = "Data Source=.\\SQLExpress;" + "Trusted_Connection=True;" + "AttachDbFilename=C:\\Temp\\Dokumenty\\Uczelnia\\Application Development\\Coursework2\\Coursework2\\Try\\Menu\\Menu\\Users.mdf;";
SqlDataAdapter da = new SqlDataAdapter();
da.InsertCommand = new SqlCommand("INSERT INTO tblScene VALUES(@CrimeSceneID, @InvestigatorID, @DataArtefactID)", cs);
da.InsertCommand.Parameters.Add("@CrimeSceneID", SqlDbType.VarChar).Value = crime;
da.InsertCommand.Parameters.Add("@InvestigatorID", SqlDbType.VarChar).Value = invest;
da.InsertCommand.Parameters.Add("@DataArtefactID", SqlDbType.VarChar).Value = "hello";
cs.Open();
da.InsertCommand.ExecuteNonQuery();
cs.Close();
da.Dispose();
return "OK";
}
catch (Exception ex)
{
// return the error message if the operation fails
return ex.Message.ToString();
}
我试图关闭我的cs连接,甚至试图处理DataAdapter-没有运气。在两次尝试重建整个项目后,我终于可以访问数据库并确认我试图保存的所有内容都在那个里。
我错过什么了吗?
谢谢修改您的代码以使用。然后,当达到使用结束时,您的连接将自动被丢弃
[WebMethod]
public string GetValues(string value, string value2)
{
try
{
string crime = value;
string invest = value2;
using(SqlConnection cs = new SqlConnection("Data Source=.\\SQLExpress;" + "Trusted_Connection=True;"))
{
using(SqlDataAdapter da = new SqlDataAdapter())
{
da.InsertCommand = new SqlCommand("INSERT INTO tblScene VALUES(@CrimeSceneID, @InvestigatorID, @DataArtefactID)", cs);
da.InsertCommand.Parameters.Add("@CrimeSceneID", SqlDbType.VarChar).Value = crime;
da.InsertCommand.Parameters.Add("@InvestigatorID", SqlDbType.VarChar).Value = invest;
da.InsertCommand.Parameters.Add("@DataArtefactID", SqlDbType.VarChar).Value = "hello";
cs.Open();
da.InsertCommand.ExecuteNonQuery();
}
}
return "OK";
}
catch (Exception ex)
{
// return the error message if the operation fails
return ex.Message.ToString();
}
}
请尝试以下代码:
[WebMethod]
public string GetValues(string value, string value2)
{
try
{
string crime = value;
string invest = value2;
using (SqlConnection cs = new SqlConnection())
{
cs.ConnectionString = "Data Source=.\\SQLExpress;" + "Trusted_Connection=True;" + "AttachDbFilename=C:\\Temp\\Dokumenty\\Uczelnia\\Application Development\\Coursework2\\Coursework2\\Try\\Menu\\Menu\\Users.mdf;";
using (SqlDataAdapter da = new SqlDataAdapter())
{
da.InsertCommand = new SqlCommand("INSERT INTO tblScene VALUES(@CrimeSceneID, @InvestigatorID, @DataArtefactID)", cs);
da.InsertCommand.Parameters.Add("@CrimeSceneID", SqlDbType.VarChar).Value = crime;
da.InsertCommand.Parameters.Add("@InvestigatorID", SqlDbType.VarChar).Value = invest;
da.InsertCommand.Parameters.Add("@DataArtefactID", SqlDbType.VarChar).Value = "hello";
cs.Open();
da.InsertCommand.ExecuteNonQuery();
}
}
return "OK";
}
catch (Exception ex)
{
// return the error message if the operation fails
return ex.Message.ToString();
}
}
我在这里看到一些主要问题,如果这些代码中的任何一个在连接打开时抛出异常,它将成为内存泄漏。使用
using
语句启动连接,或者至少使用包含finally
语句的try/catch块。您也不应该返回错误消息,您应该抛出ex代码>是否尝试使用静态连接?我收到消息:无法打开用户默认数据库。登录失败。用户“桌面PC\administrator”登录失败。数据库是在主项目中通过基于Add\NewItem\Service的数据库创建的。我想我需要创建一个用户才能登录?当我删除“Trusted_Connection=True”时,它现在显示:用户“”的登录失败。有人帮忙吗?我对数据库不是很在行。有自定义用户可以登录吗?没有,运气不好。我注意到以前数据库路径有点缺失。事实并非如此。我认为代码可能很好。我想我可能把数据库搞砸了?所以这些解决方案都没有解决我的问题。虽然我的原始代码并不完美,但不知何故,值被记录在数据库中。现在,当我使用try/catch或“using”时,我不断收到关于无法以用户身份登录的错误:“dektop PC\Administrator”(我的机器名/管理员)。有人能理解吗?:)