SqlConnection进行开关的问题
如下面 想问下C#写的操作类 tb_EmpInfoMenthod 其中 添加,修改,删除每次操作都要对SqlConnection进行开关,想问下这样是不是对系统开销很大呀,影响系统效率,有没有不
用每次都进行开关的解决办法呀?
public class tb_EmpInfoMenthod
{
SqlConnection conn = null;
SqlCommand cmd = null;
SqlDataReader qlddr = null;
#region 添加
public int tb_EmpInfoAdd(tb_EmpInfo Empinfo)
{
int intFalg = 0;
try
{
string str_Add = "insert into tb_EmpInfo values( ";
str_Add += " '" + Empinfo.intEmpId + "','" + Empinfo.strEmpName + ")";
getSqlConnection getConnection = new getSqlConnection();
conn = getConnection.GetCon();
cmd = new SqlCommand(str_Add, conn);
intFalg = cmd.ExecuteNonQuery();
conn.Dispose();
return intFalg;
}
catch (Exception ee)
{
MessageBox.Show(ee.ToString());
return intFalg;
}
}
#endregion
#region 修改
public int tb_EmpInfoUpdate(tb_EmpInfo Empinfo)
{
int intFalg = 0;
try
{
string str_Update = "update tb_EmpInfo set ";
str_Update += "EmpName='" + Empinfo.strEmpName + " where EmpId='" + Empinfo.intEmpId + "'";
getSqlConnection getConnection = new getSqlConnection();
conn = getConnection.GetCon();
cmd = new SqlCommand(str_Update, conn);
intFalg = cmd.ExecuteNonQuery();
conn.Dispose();
return intFalg;
}
catch (Exception ee)
{
MessageBox.Show(ee.ToString());
return intFalg;
}
}
#endregion
#region 删除
public int tb_EmpInfoDelete(tb_EmpInfo Empinfo)
{
int intFalg = 0;
try
{
string str_Update = "update tb_EmpInfo set ";
str_Update += "EmpFalg='" + Empinfo.intEmpFalg + "' where EmpId='" + Empinfo.intEmpId + "'";
getSqlConnection getConnection = new getSqlConnection();
conn = getConnection.GetCon();
cmd = new SqlCommand(str_Update, conn);
intFalg = cmd.ExecuteNonQuery();
conn.Dispose();
return intFalg;
}
catch (Exception ee)
{
return intFalg;
}
}
#endregion
} --------------------编程问答-------------------- 虽然c#会提供连接池,但是如果你是常用的,比较频繁,可以考虑做一个公共的可访问连接,如果不频繁,这样也可以 --------------------编程问答-------------------- 修改下你的函数,比如tb_EmpInfoAdd,添加一个参数,类型为SqlConnection,这样你就可以外部控制连接的开启与关闭,如果要灵活点,函数内部也可以判断下是否传入连接,没有传入就内部创建。 --------------------编程问答-------------------- 对于Sql server而言,连接池的作用非常明显:Open/Close一次花费的时间几乎可以忽略不计。 --------------------编程问答-------------------- 如果只是你自己一个人用,那没什么事,一直开着都没问题 ,要是多人使用。
最好用完关掉,如果连接池最多10个,那你前面10个都开着,到第11个,就开不了了。超时异常。
补充:.NET技术 , C#