110,561
社区成员
发帖
与我相关
我的任务
分享
using (SqlConnection connection = new SqlConnection(connectionString))
{
} 大家应该都是在调用时重新NEW
public class DbHelper
{
#region 私有构造和私有变量
/// <summary>
/// 私有构造函数
/// </summary>
private DbHelper()
{
}
/// <summary>
/// 获取一个数据库连接对象
/// </summary>
private static SqlConnection GetConn()
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnString"].ToString());
con.Open():
return con;
}
#endregion
#region 返回受影响的行数
/// <summary>
/// 按照指定的语句对数据表进行操作,返回受影响的行数
/// </summary>
/// <param name="sqlStr">操作语句</param>
/// <returns>受影响的行数</returns>
public static int ExecuteNonQuery(string sqlStr)
{
int mark = 0;
SqlConnection con=null;
try
{
con=GetConn();
SqlCommand cmd = new SqlCommand(sqlStr, con);
mark = cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
PubFunction.ErrorLogPrint(sqlStr, ex.ToString());
}
finally
{
if (con!=null)
{
con.Close();
con.Dispose();
}
}
return mark;
}
}
}