SQLHelper for C#.net

Source: Internet
Author: User

public class Sqlitehelper
{
Get connection string
private static readonly string str = configurationmanager.connectionstrings["Constr"]. ConnectionString;
<summary>
Adding and deleting functions
</summary>
&LT;SP name= "SQL" >sql statements </sp>
&LT;SP name= "SP" >sql parameters </sp>
<returns> number of rows affected </returns>
public static int ExecuteNonQuery (String sql, params sqliteparameter[] sp)
{
using (sqliteconnection con=new sqliteconnection (str))
{
using (Sqlitecommand cmd=new Sqlitecommand (Sql,con))
{
Con. Open ();
if (param!=null)
{
Cmd. Parameters.addrange (SP);
}
return CMD. ExecuteNonQuery ();
}
}
}
<summary>
Inquire
</summary>
&LT;SP name= "SQL" >sql statements </sp>
&LT;SP name= "param" > Parameters </sp>
<returns> return to first column </returns>
public static Object ExecuteScalar (String sql, params sqliteparameter[] sp)
{
using (sqliteconnection con=new sqliteconnection (str))
{
using (Sqlitecommand cmd=new Sqlitecommand (Sql,con))
{
Con. Open ();
if (param!=null)
{
Cmd. Parameters.addrange (SP);
}
return CMD. ExecuteScalar ();
}
}
}
<summary>
Query table
</summary>
&LT;SP name= "SQL" >sql statements </sp>
&LT;SP name= "param" > Parameters </sp>
<returns> Back to Table</returns>
public static DataTable executetable (String sql, params sqliteparameter[] sp)
{
DataTable dt = new DataTable ();
using (Sqlitedataadapter sda=new sqlitedataadapter (SQL,STR))
{
if (param!=null)
{
Sda. SelectCommand.Parameters.AddRange (SP);
}
Sda. Fill (DT);
}
return DT;
}
<summary>
Inquire
</summary>
&LT;SP name= "SQL" >sql statements </sp>
&LT;SP name= "param" > Parameters </sp>
<returns> Data </returns>
public static Sqlitedatareader ExecuteReader (String sql, params sqliteparameter[] sp)
{
Sqliteconnection con = new sqliteconnection (str);
using (Sqlitecommand cmd=new Sqlitecommand (Sql,con))
{
if (param!=null)
{
Cmd. Parameters.addrange (SP);
}
Try
{
Con. Open ();
return CMD. ExecuteReader (commandbehavior.closeconnection);
}
catch (Exception ex)
{
Con. Close ();
Con. Dispose ();
Throw ex;
}
}
}

}

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.