Simple DBHelper class in ADO (add-and-revise)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.