Write a database statistic function first
Copy Code code as follows:
public static int Count (string cityname)
{
String cmdtext = "";
SqlConnection conn = new SqlConnection (DBH. DBA);
SqlCommand cmd = null;
Cmdtext = "SELECT count (*) from [drugstore] Where cityname= @cityName";
cmd = new SqlCommand (CMDTEXT, conn);
Cmd. Parameters.addwithvalue ("@cityName", CityName);
Conn. Open ();
int total = (int) cmd. ExecuteScalar ();
Conn. Close ();
return total;
}
At the beginning of all function calls are written out, and later familiar with the use of sqlhelp convenient many, in the later direct use of dynamic soft. NET code generator, found that the original of these more and more unfamiliar, now roughly sorted, and the above repeated code part of the omitted.
1. Add Data
Copy Code code as follows:
Drugstoreinfo info = new Drugstoreinfo ();
Cmd. Parameters.addwithvalue ("@ID", info.id);
Try
{
Conn. Open ();
return CMD. ExecuteNonQuery ();
}
Catch
{
Throw
}
Finally
{
Conn. Close ();
}
. DataSet Data Paging
Copy Code code as follows:
public static DataSet indexquery (int pageIndex, int pageSize)
{
..
SqlDataAdapter da = new SqlDataAdapter (cmd);
DataSet ds = new DataSet ();
Da. Fill (ds, (pageIndex-1) * pageSize, PageSize, "Table1");
return DS;
}
. Get maximum Value
Copy Code code as follows:
public static int Getmax ()
{
String cmdtext = "Select Max (Id) from application";
..
Try
{
Conn. Open ();
Object obj = cmd. ExecuteScalar ();
if (obj = null | | obj is DBNull)
{
return 1;
}
return (int) obj + 1;
}
Catch
{
Throw
}
Finally
{
Conn. Close ();
}
}
. Select Select
Copy Code code as follows:
public static applicationinfo Select (int id)
{
String cmdtext = "Select ID from Application where id= @ID";
SqlConnection conn = new SqlConnection (DBH. connstring);
SqlCommand cmd = new SqlCommand (CMDTEXT, conn);
ApplicationInfo info = new ApplicationInfo ();
Cmd. Parameters.addwithvalue ("@ID", ID);
Conn. Open ();
using (IDataReader dr = cmd. ExecuteReader ())
{
if (Dr. Read ())
{
info.id = (int) dr["ID"];
}
Dr. Close ();
}
Conn. Close ();
return info;
}
. Delete Delete
Copy Code code as follows:
public static int Del (int id)
{
String cmdtext = "Delete from application Where id= @ID";
SqlConnection conn = new SqlConnection (DBH. connstring);
SqlCommand cmd = new SqlCommand (CMDTEXT, conn);
Cmd. Parameters.addwithvalue ("@ID", ID);
Conn. Open ();
return CMD. ExecuteNonQuery ();
Conn. Close ();
}
. Update modification
Copy Code code as follows:
public static int Update (ApplicationInfo info)
{
String cmdtext = "Update application Set city= @city Where id= @ID";
SqlConnection conn = new SqlConnection (DBH. connstring);
SqlCommand cmd = new SqlCommand (CMDTEXT, conn);
Cmd. Parameters.addwithvalue ("@ID", info.id);
Try
{
Conn. Open ();
return CMD. ExecuteNonQuery ();
}
Catch
{
Throw
}
Finally
{
Conn. Close ();
}
}
. Configure
Copy Code code as follows:
public class DBH
{
Private DBH () {}
private static readonly string _dba = configurationmanager.connectionstrings["DBA". ConnectionString;
public static string DBA
{
get {return _dba;}
}
}
Attachment: ConfigurationManager requires a dual operation of the namespace using System.Configuration and the Add Reference system.configuration.