Simple and practical data reading and data operation methods, paste them into the program and directly call
the reading method:
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;
}
Operation method:
//Execute query and return 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;
}
//Operate the database and do not return results
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;
}