asp.net SQL database Processing function Command _ Practical Tips

Source: Internet
Author: User
Tags dba
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.
Related Article

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.