日期:2008-11-11 浏览次数:20724 次
简单实用的数据读取,数据操作方法,贴到程序里面直接调用
读取方法:
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;
}