0

Web サービスに関する別の問題。今回は、WebMethod を使用して 2 つの文字列を渡し、それらをデータベースに保存しようとしています。ここまでは順調ですね。すべて正常に動作します。しかし....しかし、アプリケーションを閉じて、そのデータベース/テーブルのエラーメッセージからデータを表示したい場合、データベースは他のアプリケーションによってまだ使用されていると言い続けます。ここに私のWebServiceコードがあります:

[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を破棄しようとしました-運が悪いです。プロジェクト全体を 2 回再構築しようとした後、最終的にデータベースにアクセスして、保存しようとしていたものがすべてそこにあることを確認できます。何か不足していますか?ありがとう

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 に答える