简单实用的数据读取,数据操作方法,贴到程序里面直接调用
读取方法:
public dataset query(string sqltext)
{
sqlconnection myconnection=new sqlconnection(configurationsettings.appsettings["conn"]);
dataset ds = new dataset();
sqldataadapter mycommand = new sqldataadapter(sqltext,myconnection);
mycommand.fill(ds);
return ds;
}
操作方法:
//执行查询,返回sqldatareader
public sqldatareader querybyreader(string sqlstr)
{
if (sqlstr==string.empty)
return null;
sqlconnection myconnection = new sqlconnection(configurationsettings.appsettings["connstring"]);
sqlcommand mycommand = new sqlcommand(sqlstr, myconnection);
myconnection.open();
sqldatareader result = mycommand.executereader(commandbehavior.closeconnection);
return result;
}
//操作数据库,不返回结果
public bool operatedb(string sqlstr)
{
if (sqlstr==string.empty)
return false;
sqlconnection myconnection = new sqlconnection(configurationsettings.appsettings["connstring"]);
sqlcommand mycommand = new sqlcommand(sqlstr, myconnection);
myconnection.open();
mycommand.executenonquery();
myconnection.close();
return true;
}
新闻热点
疑难解答