0

Web服务的另一个问题。这次我使用 WebMethod 传递了两个字符串,并尝试将它们保存到数据库中。到目前为止,一切都很好。一切正常。但是....但是当我关闭应用程序并想要显示该数据库/表中的数据时,错误消息一直说数据库仍被其他应用程序使用。这是我的网络服务代码:

[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 - 不走运。在尝试重建整个项目两次之后,我终于可以访问数据库并确认我试图保存的所有内容都在那里。我错过了什么吗?谢谢

4

2 回答 2

1

试试下面的代码:

    [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();
        }
    }
于 2012-11-09T21:29:28.990 回答
0

修改您的代码以使用 USING。然后,当 USING 结束时,您的连接将自动处理。

[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();
            }
}
于 2012-11-09T20:57:33.910 回答