/*************************************************************************************************************
* 功能:数据库操作相关
* 作者:flashicp
* 时间:2005-7-22
* **********************************************************************************************************/
using system;
using system.data;
using system.data.sqlclient;
using system.web;
namespace moban
{
public class data
{
protected static string con="server=[yourserver];database=[yourdata];user id=[your id];password=[youpassword]";
//数据库字符串
protected system.data.sqlclient.sqlconnection dataconnection;
//数据库连接对象
protected system.data.sqlclient.sqldataadapter dataadapter;
//sqldataadapter对象
protected system.data.sqlclient.sqlcommand datacommand;
//sqlcommand对象
protected system.data.sqlclient.sqlcommandbuilder datacombuilder;
//sqlcommandbuilder对象
protected system.data.dataset dataset;
//dataset对象
protected system.data.datarow datarow;
//datarow对象
protected system.data.sqlclient.sqldatareader datareader=null;
//构造函数
public data()
{
dataconnection=dataconn();
}
//连接数据库参数
public string con
{
get{return con;}
set{con= value;}
}
//数据库连接参数
public sqlconnection dataconn()
{
dataconnection=new sqlconnection(con);
return dataconnection;
}
//返回数据库连接对象
public sqldatareader dataquery(string sql)
{
sql=sql.replace("--","");
sql=sql.replace(";","");
dataconnection=dataconn();
dataconnection.open();
datareader=null;
datacommand=new sqlcommand(sql,dataconnection);
datareader=datacommand.executereader();
datacommand.dispose();
return datareader;
}
//返回dataset
public virtual dataset datasetquery(string sql,string tablename)
{
dataconnection=dataconn();
dataadapter=new sqldataadapter(sql,dataconnection);
dataset=new dataset();
dataadapter.fill(dataset,tablename);
dataadapter.dispose();
dataconnection.close();
dataconnection.dispose();
return dataset;
}
//返回dataset
public dataset datasetquery(dataset myds,string tablename,string sql)
{
dataconnection=dataconn();
dataadapter=new sqldataadapter(sql,dataconnection);
dataadapter.fill(myds,tablename);
dataconnection.close();
dataconnection.dispose();
return dataset;
}
//返回dataset
public dataset datasetquery(string sql,string tablename,int starindex,int count)
{
dataconnection=dataconn();
dataconnection.open();
dataadapter=new sqldataadapter(sql,dataconnection);
dataset=new dataset();
dataadapter.fill(dataset,starindex,count,tablename);
dataconnection.close();
dataconnection.dispose();
return dataset;
}
public void datado(string sqlstr)
{
dataconnection=dataconn();
sqlcommand mycom=dataconnection.createcommand();
mycom.commandtext=sqlstr;
dataconnection.open();
mycom.executenonquery();
dataconnection.close();
}
//返回dataset 开始行到结束行
public bool editdata(string sql)
{
dataconnection=dataconn();
dataconnection.open();
datacommand=new sqlcommand(sql,dataconnection);
try
{
datacommand.executenonquery();
dataconnection.close();
return true;
}
catch(exception ex)
{
system.console.write(ex.message.tostring());
dataconnection.close();
return false;
}
}
//执行成功返回真,否则返回假.
public object executescalar(string sql)
{
dataconnection=dataconn();
dataconnection.open();
datacommand=new sqlcommand(sql,dataconnection);
try
{
object back = datacommand.executescalar();
dataconnection.close();
return back;
}
catch(exception ex)
{
system.console.write(ex.message.tostring());
dataconnection.close();
return null;
}
}
//返回一个值的查询语句
public dataset editdata(dataset dataset,sqldataadapter myda,string tablename)
{
datacombuilder=new sqlcommandbuilder(myda);
myda.updatecommand=datacombuilder.getupdatecommand();
myda.deletecommand=datacombuilder.getdeletecommand();
myda.insertcommand=datacombuilder.getinsertcommand();
myda.update(dataset,tablename);
dataset.acceptchanges();
this.closeconnection();
return dataset;
}
//修改dataset 并且修改数据库值存储过程
public bool dataprocedure(string procedurename)
{
dataconnection=dataconn();
dataconnection.open();
datacommand=new sqlcommand();
datacommand.commandtext=procedurename;
datacommand.connection=dataconnection;
datacommand.commandtype=system.data.commandtype.storedprocedure;
try
{
datacommand.executenonquery();
return true;
}
catch(exception ex)
{
system.console.write(ex.message.tostring());
return false;
}
}
//无参数存储过程的执行 返回执行真假
public void closeconnection()
{
if(datareader!=null)
{
datareader.close();
datareader=null;
}
dataconnection.close();
dataconnection.dispose();
}
//关闭连接
}
}
新闻热点
疑难解答