簡單實用的數據讀取,數據操作方法,貼到程序裡面直接調用
讀取方法:
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;
}