當我運行我的應用程序時,會發生此問題。 Express Profiler顯示與數據庫的連接發生兩次,所以問題出現在代碼中!爲什麼C#中的值在sql server數據庫中存儲兩次?
代碼示例我在這裏定義發送的內容。
label_Map.Text = message.Substring(21, 3);
label_Sys.Text = message.Substring(15, 3);
label_Dia.Text = message.Substring(18, 3);
label_Pulse.Text = message.Substring(26, 3);
//System.IO.File.WriteAllText(@"D:\String.txt", label_Sys.Text);
//System.IO.File.WriteAllText(@"D:\String.txt", label_Dia.Text);
//System.IO.File.WriteAllText(@"D:\String.txt", label_Pulse.Text);
SaveData(
message.Substring(15, 3),
message.Substring(18, 3),
message.Substring(26, 3)
//label_Sys.Text,
//label_Dia.Text,
//label_Pulse.Text
和發送代碼在這裏
private void SaveData(string sys, string dia, string pulse)
{
try
{
string connectionString = @"Data Source=PlUTO-PC\;Initial Catalog=merisana;Integrated Security=True";
using (SqlConnection connection = new SqlConnection(connectionString))
{
// connection.Open();
string queryString = "INSERT INTO dbo.merisana_test (sys, dia, pulse) VALUES (@sys, @dia, @pulse)";
SqlCommand command = new SqlCommand(queryString, connection);
//command.CommandText = string.Format("INSERT INTO merisana1 (sys, dia, pulse) VALUES ({0}, {1}, {2})", sys, dia, pulse);
command.Parameters.AddWithValue("@sys", sys);
command.Parameters.AddWithValue("@dia", dia);
command.Parameters.AddWithValue("@pulse", pulse);
command.Connection.Open();
command.ExecuteNonQuery();
}
}
catch (SqlException ex)
{
Console.WriteLine(ex.Message);
}
}
全部代碼這裏https://www.dropbox.com/s/4oitl0p3ldcfo79/Form1.cs?v=0swn-
設置一個斷點並遍歷代碼,看看它在做什麼。 – zimdanen
debug&log:在'SaveData'方法中設置斷點並計算該方法命中的次數。這是一個ASP.NET應用程序或桌面應用程序嗎? –
你可能調用過兩次這個方法嗎? –