private static string connstring = "server=.; Database=hotel;uid=aa;pwd=123 ";//preferably removed from the configuration file
public static SqlConnection conn = new SqlConnection (connstring);
The method of adding and deleting public
public static bool Zhengshangai (String sql)
{
BOOL flag = FALSE;
SqlCommand com = null;
Try
{
if (sql = = NULL | | sql = = "")
{
return false;
}
IF (Conn. State = ConnectionState.Open)
Conn. Open ();
com = new SqlCommand (SQL, conn);
if (COM. ExecuteNonQuery () > 0)
Flag = true;
}
catch (Exception ex)
{
Throw ex;
}
Finally
{
Com. Dispose ();
Conn. Close ();
}
return flag;
}
Common Query method with a return value of Sqldatereader
public static SqlDataReader Query (String sql)
{
SqlCommand com = null;
SqlDataReader reader = null;
Try
{
if (sql = = NULL | | sql = = "")
{
return null;
}
IF (Conn. State = ConnectionState.Open)
Conn. Open ();
com = new SqlCommand (SQL, conn);
Connection closes automatically when reader is turned off
Reader = com. ExecuteReader (commandbehavior.closeconnection);
}
catch (Exception ex)
{
return null;
Throw ex;
}
return reader;
}
Common Query method, the return value is DataTable;
public static DataTable QueryInfo (String sql)
{
DataTable dt = new DataTable ();
SqlDataAdapter da = null;
Try
{
DBHelper.conn.Open ();
da = new SqlDataAdapter (SQL, dbhelper.conn);
Da. Fill (DT);
}
catch (Exception e)
{
Console.WriteLine (E.message);
}
Finally
{
DBHelper.conn.Close ();
}
return DT;
}
How to execute a stored procedure with parameters
public bool Excuteproc (string procname, sqlparameter[] procparams)
{
BOOL flag = FALSE;
SqlCommand com = null;
Try
{
if (procname = = NULL | | procname = = "") return false;
IF (Conn. state! = ConnectionState.Open) Conn. Open ();
com = new SqlCommand (procname, conn);
Com.commandtype = CommandType.StoredProcedure;
if (procparams! = null)
{
foreach (SqlParameter procparam in Procparams)
{
Com. Parameters.Add (Procparam);
}
}
if (COM. ExecuteNonQuery () > 0) flag = true;
}
catch (Exception)
{
return false;
}
Finally
{
Com. Dispose ();
Conn. Close ();
}
return flag;
}
}
The above is very simple ADO basic Help class, if there is any doubt, or think I write wrong, I would like to communicate with you
Simple DBHelper class in ADO (add-and-revise)