<connectionStrings>
<add name="strCon" connectionString="Data Source=.;Initial Catalog=DataManager;User ID=sa;Passwoed=lifatshit"/>
</connectionStrings>
封装表格
public static DataTable ExecuteDateTable(string sql, params SqlParameter[] param)
{
DataTable dt = null;
using(SqlConnection con=new SqlConnection(strCon))
{
SqlCommand cmd = new SqlCommand(sql, con);
cmd.Parameters.AddRange(param);
using(SqlDataAdapter adapter=new SqlDataAdapter(cmd))
{
dt = new DataTable();
adapter.Fill(dt);
}
}
return dt;
}
//执行增删改 返回受影响的行数
public static int ExecuteNonquery(string sql, params SqlParameter[] param)
{
int n = -1;
using (SqlConnection con = new SqlConnection(strCon))
{
using (SqlCommand cmd = new SqlCommand(sql, con))
{
con.Open();
cmd.Parameters.AddRange(param);
n = cmd.ExecuteNonQuery();
}
}
return n;
}
//执行查询 负责返回首行首列
public static object ExecuteDcalar(string sql, params SqlParameter[] param)
{
object o = null;
using(SqlConnection con=new SqlConnection (strCon))
{
using(SqlCommand cmd=new SqlCommand (sql,con))
{
con.Open();
cmd.Parameters.AddRange(param);
o = cmd.ExecuteScalar();
}
}
return o;
}
//读取数据库
public static SqlDataReader ExecuteReader(string sql,params SqlParameter[] param)
{
SqlDataReader reader = null;
using (SqlConnection con = new SqlConnection(strCon))
{
using (SqlCommand cmd = new SqlCommand(sql, con))
{
con.Open();
cmd.Parameters.AddRange(param);
reader = cmd.ExecuteReader();
}
}
return reader;
}