这段时间闲下来写了一些东西,重新写了一个简单的数据库帮组类
public class MyDBHelper
{
public static readonly string connString = ConfigurationManager.ConnectionStrings["mydb"].ToString();
/// <summary>
/// 查询
/// </summary>
/// <param name="sql"></param>
/// <param name="parm"></param>
/// <returns></returns>
public static DataTable GetInfo(string sql,params SqlParameter[] parm)
{
//DataTable dt = new DataTable();
//using (SqlConnection conn = new SqlConnection(connString))
//{
// using (SqlCommand cmd = new SqlCommand(sql, conn))
// {
// cmd.CommandType = CommandType.Text;
// cmd.Parameters.AddRange(parm);
// SqlDataAdapter dap = new SqlDataAdapter(cmd);
// dap.Fill(dt);
// dap.Dispose();
// }
//}
//return dt;
using (SqlConnection conn = new SqlConnection(sql))
{
using (SqlDataAdapter dap = new SqlDataAdapter(sql, conn))
{
dap.SelectCommand.Parameters.AddRange(parm);
DataTable dt = new DataTable();
dap.Fill(dt);
return dt;
}
} }
/// <summary>
/// 增删改
/// </summary>
/// <param name="sql"></param>
/// <param name="parm"></param>
/// <returns></returns>
public static bool Commad(string sql,params SqlParameter[] parm)
{
using (SqlConnection conn = new SqlConnection(connString))
{
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddRange(parm);
int h = cmd.ExecuteNonQuery();
if (h > )
{
return true;
}
else
{
return false;
}
}
} } }