簡(jiǎn)單實(shí)用的數(shù)據(jù)讀取,數(shù)據(jù)操作方法,貼到程序里面直接調(diào)用
讀取方法:
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; }
操作方法:
//執(zhí)行查詢,返回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; }
//操作數(shù)據(jù)庫,不返回結(jié)果 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; }
|