C#数据库操作通⽤类(增删改查)
数据库操作通⽤类
优点:达到了sql防注⼊,并且,将对数据库的操作独⽴开来,可以提⾼内聚,降低耦合。达到了哪⾥使⽤,哪⾥关闭的效果,避免隐患。
public class SqlHelper
{
SqlConnection conn = null;
SqlCommand com = null;
SqlDataReader rd = null;
string constr = System.Configuration.ConfigurationManager.ConnectionStrings["my_connect"].ToString();
public bool ConnectSql()
{// 连接数据库
try
{
conn =new SqlConnection(constr);
conn.Open();
return true;
}
catch
{
return false;
}
}
基本的增删改查语句public bool SqlPour(string sql, Dictionary<string, string> dic)
{//可完成增删改
try
{
ConnectSql();//打开连接
com =new SqlCommand(sql, conn);
if(dic != null)
{
foreach(var item in dic)
{
com.Parameters.AddWithValue(item.Key, item.Value);
}
}
com.ExecuteNonQuery();
return true;
}
catch(Exception e)
{
Console.WriteLine(e.Message);
return false;
}
finally//关闭连接
{
closeConn();
}
}
public ArrayList SelectInfo(string sql, Dictionary<string, string> dic)
{//可完成查操作,以Object存取放⼊ArrayList返回
try
{
ConnectSql();//打开连接
com =new SqlCommand(sql, conn);
ArrayList al =new ArrayList();
if(dic != null)
{
foreach (var item in dic)
{//遍历参数并进⾏赋值,防⽌sql注⼊
com.Parameters.AddWithValue(item.Key, item.Value);
}
}
rd = com.ExecuteReader();
int clumn =0;//得到数据的列数
if(rd.Read())
{
clumn = rd.FieldCount;
}
else
{
return null;
}
do
{//读取每⾏每列的数据并放⼊Object数组中
Object[] obj =new object[clumn];
for(int i =0; i < clumn; i++)
{
obj[i]= rd[i];
}
al.Add(obj);//将⼀⾏数据放⼊数组中
}while(rd.Read());
return al;
}
catch
{
return null;
}
finally
{
closeConn();
}
}
public void closeConn()
{//关闭数据库连接
try
{
if(conn != null){ conn.Close();}
if(rd != null){ rd.Close();}
}
catch
{
return;
}
}
}
使⽤例⼦
插⼊操作
Dictionary<string, string> dic =new Dictionary<string, string>();
insert_sql ="insert into Vote values(@vusername, @vname, @vtime)";//放置占位符dic.Add("@vusern
ame", username);//将其放⼊字典(类似JSON,采⽤键值对的⽅式传递)dic.Add("@vname", vname);
dic.Add("@vtime", DateTime.Now.ToString());
if(!sqlHelper.SqlPour(insert_sql, dic)){return false;}
更新
Dictionary<string, string> dic =new Dictionary<string, string>();
update_sql ="update Vote set Vname=@vname, Vtime=@vtime where Vusername=@vusername";
dic.Add("@vname", vname);
dic.Add("@vtime", DateTime.Now.ToString());
dic.Add("@vusername", username);
if(!sqlHelper.SqlPour(update_sql, dic)){return false;}
如果你的sql语句不需要占位符,那就不需要占位符
string sql ="DELETE FROM Person";
if(!sqlHelper.SqlPour(update_sql, null)){return false;}
切记不可这样使⽤
string sql ="DELETE FROM @Person";
Dictionary<string, string> dic =new Dictionary<string, string>();
dic.Add("@Person", table_name);
if(!sqlHelper.SqlPour(update_sql, dic)){return false;}
报错原因:可以看SqlHelper中Parameters.AddWithValue(key, value),它会在sql语句中⾃动加上’’,也就是说这句话最后会变成DELETE FROM 'Person',⾃然报错。
string sql ="select * from Student where id=@id";
Dictionary<string, string> dic =new Dictionary<string, string>();
dic.Add("@id", id);
ArrayList al = sqlHelper.SelectInfo(sql, dic);
///...判断是否为空跳过,请⾃⾏检查,如果报错异常、得到数据为空,则返回null
foreach(Object[] obj in al){//得到⼏⾏数据就能产⽣多少个对象
Student stu =new Student();
stu.id =(int)obj[0];
//强转成你需要的数据,强转很容易发⽣异常,所以数据库的约束性要强,对象类时要认真检查数据类型,多⽤atch
stu.name =(string)obj[1];
...
}
总结:可以看出,增删改的操作时⼤同⼩异的,可以总结使⽤⽅法,⽤在各个地⽅进⾏通⽤,不⽤多次编写重复代码。
如果有错误,还请⼤家指教,有错轻喷,谢谢⼤家

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。