首页 > 开发 > 综合 > 正文

一个数据操作底层类

2024-07-21 02:23:24
字体:
来源:转载
供稿:网友
我对这个类不是很满意,如果你有改进意见请联系我,不胜感激.

主要将对数据库的操作简单封装了一下

using system;
using system.collections;
using system.componentmodel;
using system.data;
using system.diagnostics;
using system.configuration;
using system.data.sqlclient;

namespace addr.admin.executesqlcommand
{
?///
?/// executecommand 的摘要说明:数据处理层。
?///
?public class executecommand
?{
??string cnstr;
??string errormessage="";
??bool successful=true;
??//构造函数将连接字符串赋给变量
??public executecommand()
??{
???//
???// todo: 在此处添加构造函数逻辑
???//
???cnstr=configurationsettings.appsettings["cn"];
??}
??//枚举执行sqlcommand的类型
??public enum cmdtype
??{
???executenonquery=1,
???executescalar,
???executereader,
???executedataset,
???executeid
??};
??//返回是否执行成功的属性
??public bool rlt
??{
???get
???{
????return successful;
???}
??}
??//返回错误信息
??public string msg
??{
???get
???{
????return errormessage;
???}
??}
??//执行命令的函数
??public object executing(sqlcommand cm,cmdtype type)
??{
???sqlconnection cn;
???try
???{
????cn=new sqlconnection(cnstr);
????cn.open();
????cm.connection=cn;

????switch(type)
????{
?????case cmdtype.executenonquery:
??????try
??????{
???????cm.executenonquery();
???????cn.close();
??????}
??????catch(exception ex)
??????{
???????errormessage="error executing 'executenonquery'."+ex.message;
???????successful=false;
??????}
??????break;
?????case cmdtype.executereader:
??????try
??????{
???????sqldataadapter da=new sqldataadapter(cm.commandtext,cn);
???????dataset ds=new dataset();
???????da.fill(ds,"first");
???????da.dispose();
???????cn.close();
???????return ds.tables["first"];
??????}
??????catch
??????{
???????errormessage="error executing 'executereader'.";
???????successful=false;
??????}
??????break;
?????case cmdtype.executescalar:
??????try
??????{
???????int result=(int)cm.executescalar();
???????cn.close();
???????return result;
??????}
??????catch
??????{
???????errormessage="error executing 'executescalar'.'";
???????successful=false;
??????}
??????break;
?????case cmdtype.executedataset:
??????try
??????{
???????sqldataadapter da=new sqldataadapter(cm.commandtext,cn);
???????dataset ds=new dataset();
???????da.fill(ds);
???????cn.close();
???????return ds;
??????}
??????catch
??????{
???????errormessage="error executing 'executedataset'.";
???????successful=false;
??????}
??????break;
?????case cmdtype.executeid:
??????try
??????{
???????string rst="";
???????sqldatareader dr=cm.executereader();
???????if(dr.read())
???????{
????????rst=dr["id"].tostring();
???????}
???????dr.close();
???????cn.close();
???????return rst;
??????}
??????catch
??????{
???????errormessage="error executing 'executeid'.";
???????successful=false;
??????}
??????break;
?????default:
??????errormessage="error executing unknown sqlcommand.";
??????successful=false;
??????break;
????}
????return successful;
???}
???catch
???{
????successful=false;
????errormessage="error opening data connection.";
????return successful;
???}
??}
?}
}



发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表