本文介绍了为什么在Command.ExecuteNonQuery()之后关闭连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
private static string connectionString = @ 用户ID = ****;密码= ****;主机= ****;数据库= ****;持久安全信息=真;
public MySqlConnection Connection = null ;
public MySqlTransaction Transaction = null ;
private MySqlCommand Command = null ;
public LinqDataContext( string p_ConnectionString = )
{
if ( string .IsNullOrWhiteSpace(p_ConnectionString)== false )
{
connectionString = p_ConnectionString;
}
Connection = new MySqlConnection(connectionString);
}
public int InsertPayee(System.Nullable< int> p_StoreID,System.Nullable< int> p_PayeeID, string p_PayeeName, string p_StreetAddress, string p_City,System.Nullable< int> p_StateID , string p_ZipCode, string p_PhoneNumber, string p_EmailAddress, string p_Notes,System.Nullable< int> p_CarrierID,System.Nullable< bool> p_IsActive,System.Nullable< int> p_RegisterID)
{
使用(连接)
{
if (Connection.State!= ConnectionState.Open)
{
Connection.Open();
}
Command = new MySqlCommand( InsertPayee,Connection);
Command.CommandType = CommandType.StoredProcedure;
if (Transaction!= null )
{
命令。交易=交易;
}
Command.Parameters.Add( new MySqlParameter( p_StoreID,p_StoreID));
Command.Parameters.Add( new MySqlParameter( p_PayeeID,p_PayeeID));
Command.Parameters.Add( new MySqlParameter( p_PayeeName,p_PayeeName));
Command.Parameters.Add( new MySqlParameter( p_StreetAddress,p_StreetAddress));
Command.Parameters.Add( new MySqlParameter( p_City,p_City));
Command.Parameters.Add( new MySqlParameter( p_StateID,p_StateID));
Command.Parameters.Add( new MySqlParameter( p_ZipCode,p_ZipCode));
Command.Parameters.Add( new MySqlParameter( p_PhoneNumber,p_PhoneNumber));
Command.Parameters.Add( new MySqlParameter( p_EmailAddress,p_EmailAddress));
Command.Parameters.Add( new MySqlParameter( p_Notes,p_Notes));
Command.Parameters.Add( new MySqlParameter( p_CarrierID,p_CarrierID));
Command.Parameters.Add( new MySqlParameter( p_IsActive,p_IsActive));
Command.Parameters.Add( new MySqlParameter( p_RegisterID,p_RegisterID));
return Command.ExecuteNonQuery();
}
}
解决方案
private static string connectionString = @"User Id=****;Password=****;Host=****;Database=****;Persist Security Info=True"; public MySqlConnection Connection = null; public MySqlTransaction Transaction = null; private MySqlCommand Command = null; public LinqDataContext(string p_ConnectionString = "") { if (string.IsNullOrWhiteSpace(p_ConnectionString) == false) { connectionString = p_ConnectionString; } Connection = new MySqlConnection(connectionString); }
public int InsertPayee(System.Nullable<int> p_StoreID, System.Nullable<int> p_PayeeID, string p_PayeeName, string p_StreetAddress, string p_City, System.Nullable<int> p_StateID, string p_ZipCode, string p_PhoneNumber, string p_EmailAddress, string p_Notes, System.Nullable<int> p_CarrierID, System.Nullable<bool> p_IsActive, System.Nullable<int> p_RegisterID) { using (Connection) { if (Connection.State != ConnectionState.Open) { Connection.Open(); } Command = new MySqlCommand("InsertPayee", Connection); Command.CommandType = CommandType.StoredProcedure; if (Transaction != null) { Command.Transaction = Transaction; } Command.Parameters.Add(new MySqlParameter("p_StoreID", p_StoreID)); Command.Parameters.Add(new MySqlParameter("p_PayeeID", p_PayeeID)); Command.Parameters.Add(new MySqlParameter("p_PayeeName", p_PayeeName)); Command.Parameters.Add(new MySqlParameter("p_StreetAddress", p_StreetAddress)); Command.Parameters.Add(new MySqlParameter("p_City", p_City)); Command.Parameters.Add(new MySqlParameter("p_StateID", p_StateID)); Command.Parameters.Add(new MySqlParameter("p_ZipCode", p_ZipCode)); Command.Parameters.Add(new MySqlParameter("p_PhoneNumber", p_PhoneNumber)); Command.Parameters.Add(new MySqlParameter("p_EmailAddress", p_EmailAddress)); Command.Parameters.Add(new MySqlParameter("p_Notes", p_Notes)); Command.Parameters.Add(new MySqlParameter("p_CarrierID", p_CarrierID)); Command.Parameters.Add(new MySqlParameter("p_IsActive", p_IsActive)); Command.Parameters.Add(new MySqlParameter("p_RegisterID", p_RegisterID)); return Command.ExecuteNonQuery(); } }
解决方案
这篇关于为什么在Command.ExecuteNonQuery()之后关闭连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!