using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.SqlClient; using System.Data; class sqlHelp { static string constr = "server=.;database=Dept;uid=sql;pwd=123";
static SqlConnection con = new SqlConnection(constr);
//得到一个数据 public static object GetOneData(string sql,List<SqlParameter> lt) { try { con.Open(); SqlCommand com = new SqlCommand(sql,con); if (lt != null) { foreach (SqlParameter sp in lt) { com.Parameters.Add(sp); } } object b = com.ExecuteScalar(); return b; } catch (Exception ) { throw; } finally { con.Close(); } } public static object GetOneData(string sql, List<SqlParameter> lt,CommandType type) { try { con.Open(); SqlCommand com = new SqlCommand(sql, con); com.CommandType = type; if (lt != null) { foreach (SqlParameter sp in lt) { com.Parameters.Add(sp); } } object b = com.ExecuteScalar(); return b; } catch (Exception) { throw; } finally { con.Close(); } } //执行增加、删除、修改的语句 public static int ExeSql(string sql, List<SqlParameter> lt, CommandType type) {
try { con.Open(); SqlCommand com = new SqlCommand(sql, con); com.CommandType = type; if (lt != null) { foreach (SqlParameter sp in lt) { com.Parameters.Add(sp); } } int i = com.ExecuteNonQuery(); return i; } catch (Exception) { throw; } finally { con.Close(); } } //执行查询语句,返回结果集 public static SqlDataReader GetReader(string sql, List<SqlParameter> lt, CommandType type) { try { con.Open(); SqlCommand com = new SqlCommand(sql, con); com.CommandType = type; if (lt != null) { foreach (SqlParameter sp in lt) { com.Parameters.Add(sp); } } SqlDataReader sdr = com.ExecuteReader(CommandBehavior.CloseConnection);//关闭DAtareader时,同时关闭数据连接 return sdr; } catch (Exception) { throw; }
} //执行查询语句,返回dataset结果集 public static DataSet GetDs(string sql, List<SqlParameter> lt, CommandType type) { try { con.Open(); SqlCommand com = new SqlCommand(sql, con); com.CommandType = type; if (lt != null) { foreach (SqlParameter sp in lt) { com.Parameters.Add(sp); } } SqlDataAdapter sda = new SqlDataAdapter(com); DataSet ds = new DataSet(); sda.Fill(ds);
connection.Close();
{
throw;
}
finally
{
connection.Close();
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
class sqlHelp
{
static string constr = "server=.;database=Dept;uid=sql;pwd=123";
static SqlConnection con = new SqlConnection(constr);
//得到一个数据
public static object GetOneData(string sql,List<SqlParameter> lt)
{
try
{
con.Open();
SqlCommand com = new SqlCommand(sql,con);
if (lt != null)
{
foreach (SqlParameter sp in lt)
{
com.Parameters.Add(sp);
}
}
object b = com.ExecuteScalar();
return b; }
catch (Exception )
{ throw;
}
finally
{
con.Close();
}
}
public static object GetOneData(string sql, List<SqlParameter> lt,CommandType type)
{
try
{
con.Open();
SqlCommand com = new SqlCommand(sql, con);
com.CommandType = type;
if (lt != null)
{
foreach (SqlParameter sp in lt)
{
com.Parameters.Add(sp);
}
}
object b = com.ExecuteScalar();
return b; }
catch (Exception)
{ throw;
}
finally
{
con.Close();
}
}
//执行增加、删除、修改的语句
public static int ExeSql(string sql, List<SqlParameter> lt, CommandType type)
{
try
{
con.Open();
SqlCommand com = new SqlCommand(sql, con);
com.CommandType = type;
if (lt != null)
{
foreach (SqlParameter sp in lt)
{
com.Parameters.Add(sp);
}
}
int i = com.ExecuteNonQuery();
return i;
}
catch (Exception)
{ throw;
}
finally
{
con.Close();
} }
//执行查询语句,返回结果集
public static SqlDataReader GetReader(string sql, List<SqlParameter> lt, CommandType type)
{
try
{
con.Open();
SqlCommand com = new SqlCommand(sql, con);
com.CommandType = type;
if (lt != null)
{
foreach (SqlParameter sp in lt)
{
com.Parameters.Add(sp);
}
}
SqlDataReader sdr = com.ExecuteReader(CommandBehavior.CloseConnection);//关闭DAtareader时,同时关闭数据连接
return sdr;
}
catch (Exception)
{ throw;
}
}
//执行查询语句,返回dataset结果集
public static DataSet GetDs(string sql, List<SqlParameter> lt, CommandType type)
{
try
{
con.Open();
SqlCommand com = new SqlCommand(sql, con);
com.CommandType = type;
if (lt != null)
{
foreach (SqlParameter sp in lt)
{
com.Parameters.Add(sp);
}
}
SqlDataAdapter sda = new SqlDataAdapter(com);
DataSet ds = new DataSet();
sda.Fill(ds);
return ds;
}
catch (Exception)
{ throw;
}
finally
{
con.Close();
} }
}
各位大神帮我看看,有错么?
一个connection同时只能有一个command,一个command上同时只能有一个未关闭的datareader
回答完毕