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