c 操作mysql通用類 C 資料庫操作通用類

2021-10-25 17:29:29 字數 4295 閱讀 5341

usingsystem;usingsystem.collections.generic;usingsystem.linq;usingsystem.text;usingsystem.data.common;usingsystem.data;usingsystem.reflection;namespacedbhelpers

returndbhelper.dbparafactory;

}set

}privatestaticdbconnection _connection;/publicstaticdbconnection connection

elseif(_connection.state==connectionstate.closed)

_connection.open();

}elseif(_connection.state==connectionstate.broken)

_connection.close();

_connection.open();

}return_connection;

}publicstaticboolexists(stringsafesql)

publicstaticboolexists(stringsql,paramsdbparameter values)

//執行並返回影響行數/publicstaticintexecutecommand(stringsafesql)

dbcommand cmd=cmd=dbparafactory.getdbcommand();

cmd.connection=connection;

cmd.commandtype=commandtype.text;

cmd.commandtext=safesql;intresult=cmd.executenonquery();returnresult;

}//執行並返回影響行數publicstaticintexecutecommand(stringsql,paramsdbparameter values)

dbcommand cmd=dbparafactory.getdbcommand();

cmd.connection=connection;

cmd.commandtype=commandtype.text;

cmd.commandtext=sql;

cmd.parameters.addrange(values);returncmd.executenonquery();

}//執行並返回執行結果中的第一列/publicstaticobjectgetscalar(stringsafesql)

dbcommand cmd=dbparafactory.getdbcommand();

cmd.connection=connection;

cmd.commandtype=commandtype.text;

cmd.commandtext=safesql;objectresult=cmd.executescalar();returnresult;

}//執行並返回執行結果中的第一列publicstaticobjectgetscalar(stringsql,paramsdbparameter values)

dbcommand cmd=dbparafactory.getdbcommand();

cmd.connection=connection;

cmd.commandtype=commandtype.text;

cmd.parameters.addrange(values);

cmd.commandtext=sql;objectresult=cmd.executescalar();returnresult;

}//根據sql語句獲得datareader/publicstaticdbdatareader getreader(stringsafesql)

dbcommand cmd=dbparafactory.getdbcommand();

cmd.connection=connection;

cmd.commandtype=commandtype.text;

cmd.commandtext=safesql;

dbdatareader reader=cmd.executereader();returnreader;

}//根據sql語句獲得datareaderpublicstaticdbdatareader getreader(stringsql,paramsdbparameter values)

dbcommand cmd=dbparafactory.getdbcommand();

cmd.connection=connection;

cmd.commandtype=commandtype.text;

cmd.commandtext=sql;

cmd.parameters.addrange(values);

dbdatareader reader=cmd.executereader();returnreader;

}//根據sql語句獲得datatable/publicstaticdataset getdataset(stringsafesql)

dataset ds=newdataset();

dbcommand cmd=dbparafactory.getdbcommand();

cmd.connection=connection;

cmd.commandtype=commandtype.text;

cmd.commandtext=safesql;

dbdataadapter da=dbparafactory.getdbdataadapter();

da.selectcommand=cmd;

da.fill(ds);returnds;

}//根據sql語句獲得datatablepublicstaticdataset getdataset(stringsql,paramsdbparameter values)

dataset ds=newdataset();

dbcommand cmd=dbparafactory.getdbcommand();

cmd.connection=connection;

cmd.commandtype=commandtype.text;

cmd.commandtext=sql;

cmd.parameters.addrange(values);

dbdataadapter da=dbparafactory.getdbdataadapter();

da.selectcommand=cmd;

da.fill(ds);returnds;

}publicstaticdataadapter getdataadapter(stringsafesql)

dbcommand cmd=dbparafactory.getdbcommand();

cmd.connection=connection;

cmd.commandtype=commandtype.text;

cmd.commandtext=safesql;

dbdataadapter da=dbparafactory.getdbdataadapter();

da.selectcommand=cmd;returnda;

}//根據sql語句獲得datatablepublicstaticdataadapter getdataadapter(stringsql,paramsdbparameter values)

dbcommand cmd=dbparafactory.getdbcommand();

cmd.connection=connection;

cmd.commandtype=commandtype.text;

cmd.commandtext=sql;

cmd.parameters.addrange(values);

dbdataadapter da=dbparafactory.getdbdataadapter();

da.selectcommand=cmd;returnda;

}//dataset裝換為泛型集合/dataset///待轉換資料表索引///publicstaticilistdatasettoilist(dataset ds,inttableindex)

returnresult;

}//dataset裝換為泛型集合/dataset///待轉換資料表名稱//2008-08-01 22:47 hpdv2806publicstaticilistdatasettoilist(dataset ds,stringtablename)

returndatasettoilist(ds, _tableindex);

C 通用資料庫操作類

使用ado.net的方式運算元據庫時,對於經常需要操作不同資料庫的同學,需要對不同的資料庫翻來覆去地寫操作類。對ado.net,運算元據庫需要有幾個核心的東西 以mysql為例 負責mysql的連線,在操作mysql前,需要先獲得連線。負責具體命令的類,具體需要執行的sql的語句需要放到它的comm...

C 資料庫SQL操作通用類

剛學c 自己寫了乙個資料庫操作的通用類,以做復用。寫得不好,不斷更新中。1 using system 2 using system.data 3 using system.data.sqlclient 4 5 6 namespace qxtintface7 18 catch exception ex...

c 連線mysql 通用類 資料庫通用連線類

usingsystem usingsystem.data usingsystem.data.sqlclient namespacedataproviders sqldataprovider 的摘要說明。internal classsqldataprovider idataprovider priva...