ADO. NET generic database category, general database ado.net
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. Threading. Tasks;
Using System. Data;
Using System. Data. SqlClient;
Namespace Test
{
Public class DBHelper
{
Public static string ConString = "Data Source =.; Initial Catalog = bankdb; User id = sa; Password = 123 ;";
// Execute the add, delete, and modify Method
Public static int RunNoQuery (string parameter text, CommandType parameter type, params SqlParameter [] pars)
{
SqlConnection con = new SqlConnection (ConString );
Con. Open ();
SqlCommand cmd = new SqlCommand (plain text, con );
Cmd. CommandType = primitive type;
If (pars! = Null & pars. Length> 0)
{
Foreach (SqlParameter p in pars)
{
Cmd. Parameters. Add (p );
}
}
Int rows = cmd. ExecuteNonQuery ();
Con. Close ();
Return rows;
}
// Method for executing the query (DataSet)
Public static DataSet RunSelect (string parameter text, CommandType parameter type, params SqlParameter [] pars)
{
SqlConnection con = new SqlConnection (ConString );
SqlDataAdapter da = new SqlDataAdapter (plain text, con );
Da. SelectCommand. CommandType = repeated type;
If (pars! = Null & pars. Length> 0)
{
Foreach (SqlParameter p in pars)
{
Da. SelectCommand. Parameters. Add (p );
}
}
DataSet ds = new DataSet ();
Da. Fill (ds );
Return ds;
}
// Run the query to obtain a value.
Public static object RunOneValue (string plain text, CommandType primitive type, params SqlParameter [] pars)
{
SqlConnection con = new SqlConnection (ConString );
Con. Open ();
SqlCommand cmd = new SqlCommand (plain text, con );
Cmd. CommandType = primitive type;
If (pars! = Null & pars. Length> 0)
{
Foreach (SqlParameter p in pars)
{
Cmd. Parameters. Add (p );
}
}
Object obj = cmd. ExecuteScalar ();
Con. Close ();
Return obj;
}
}
}