server|資料|資料庫
簡單實用的資料讀取,資料操作方法,貼到程式裡面直接調用
讀取方法:
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;
}