简单实用的数据读取,数据操作方法,贴到程序里面直接调用
读取方法:
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;
}
Word教程网 | Excel教程网 | Dreamweaver教程网 | Fireworks教程网 | PPT教程网 | FLASH教程网 | PS教程网 |
HTML教程网 | DIV CSS教程网 | FLASH AS教程网 | ACCESS教程网 | SQL SERVER教程网 | C语言教程网 | JAVASCRIPT教程网 |
ASP教程网 | ASP.NET教程网 | CorelDraw教程网 |