我正在使用c#应用程序从tcp客户端接收字符串。并存储在数据库(Sql server 2005)。应用程序正常工作但有时它给出错误
" executenonquery需要一个开放且可用的连接。连接的当前状态是连接"
它每天都会重新启动应用程序它会在一段时间之后正常工作,错误将会到来
当我收到此错误时,我失去了它不会在数据库中存储我的代码
try
{
if (Latitude != 0 && Longitude != 0)
{
SqlCommand cmd = new SqlCommand("Insert_Mapapp", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@Latitude", Latitude));
cmd.Parameters.Add(new SqlParameter("@Longitude ", Longitude));
cmd.Parameters.Add(new SqlParameter("@Speed", Speed));
cmd.Parameters.Add(new SqlParameter("@Battery_ADC ", Battery_ADC));
cmd.Parameters.Add(new SqlParameter("@sDateTime ", sDateTime));
cmd.Parameters.Add(new SqlParameter("@Main_Backup_Battery ", Main_Backup_Battery));
cmd.Parameters.Add(new SqlParameter("@Pulse_Counter ", Pulse_Counter));
cmd.Parameters.Add(new SqlParameter("@Number_of_Satellites ", Number_of_Satellites));
cmd.Parameters.Add(new SqlParameter("@IgnitionStatus ", IgnitionStatus));
cmd.Parameters.Add(new SqlParameter("@Condition_Inputs ", Condition_Inputs));
cmd.Parameters.Add(new SqlParameter("@FuelADC ", FuelADC));
cmd.Parameters.Add(new SqlParameter("@UID ", UnitID));
cmd.Parameters.Add(new SqlParameter("@Device_IP", remoteIP));
cmd.Parameters.Add(new SqlParameter("@Device_Port", remotePort));
cmd.Parameters.Add(new SqlParameter("@String_Count", UIDCounter));
cmd.Parameters.Add(new SqlParameter("@Stored", Stored));
// cmd.Parameters.Add("@Nofrec", SqlDbType.Int).Direction = ParameterDirection.Output;
if (cmd.Connection.State == ConnectionState.Closed)
cmd.Connection.Open();
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
AddTextToListBox(ex.Message);
AddTextToListBox(tmp);
}
finally
{
if (con.State == ConnectionState.Open)
con.Close();
}
如何解决这个问题?
答案 0 :(得分:4)
我认为您应该将if语句更改为:
if(cmd.Connection.State != ConnectionState.Open)
答案 1 :(得分:0)
try
{
if (Latitude != 0 && Longitude != 0)
{
SqlCommand cmd = new SqlCommand("Insert_Mapapp", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@Latitude", Latitude));
cmd.Parameters.Add(new SqlParameter("@Longitude ", Longitude));
cmd.Parameters.Add(new SqlParameter("@Speed", Speed));
cmd.Parameters.Add(new SqlParameter("@Battery_ADC ", Battery_ADC));
cmd.Parameters.Add(new SqlParameter("@sDateTime ", sDateTime));
cmd.Parameters.Add(new SqlParameter("@Main_Backup_Battery ", Main_Backup_Battery));
cmd.Parameters.Add(new SqlParameter("@Pulse_Counter ", Pulse_Counter));
cmd.Parameters.Add(new SqlParameter("@Number_of_Satellites ", Number_of_Satellites));
cmd.Parameters.Add(new SqlParameter("@IgnitionStatus ", IgnitionStatus));
cmd.Parameters.Add(new SqlParameter("@Condition_Inputs ", Condition_Inputs));
cmd.Parameters.Add(new SqlParameter("@FuelADC ", FuelADC));
cmd.Parameters.Add(new SqlParameter("@UID ", UnitID));
cmd.Parameters.Add(new SqlParameter("@Device_IP", remoteIP));
cmd.Parameters.Add(new SqlParameter("@Device_Port", remotePort));
cmd.Parameters.Add(new SqlParameter("@String_Count", UIDCounter));
cmd.Parameters.Add(new SqlParameter("@Stored", Stored));
// cmd.Parameters.Add("@Nofrec", SqlDbType.Int).Direction = ParameterDirection.Output;
if (cmd.Connection != ConnectionState.Open)
{
cmd.Connection.Open();
}
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw;
}
finally
{
if (cmd.Connection.ToString() != String.Empty && cmd.Connection.State != ConnectionState.Closed)
CONN.Close();
}