Use ADO. Net to access the local database under ASP. NET

Source: Internet
Author: User

 I believe that ASP. NET controls are quite comfortable to use, but I still feel that the controls have many defects. First, the portability is not very good,CodeThis is the king. The following provides the ADO. Net code. You only need to add this file to the app_code of the website project and modify several values so that you can easily access your local database.

The ADO. Net code is as follows:

Using system;
Using system. Data;
Using system. configuration;
Using system. LINQ;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. htmlcontrols;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. xml. LINQ;
Using system. Data. sqlclient;

Namespace WB
{

//


// access data in the database
// important variables in this class have been specified
///
public class dataaccess
{


Public dataaccess ()
{
//
// Todo: add the constructor logic here
//
}

# Region defines public variables
Public static string SQL _con = "Data Source = FELVEN-PC // sqlexpress ; Initial catalog =BBS ; Integrated Security = true "; // connection string
Public static string userid = ""; // after a user logs on, the user's identity must be determined based on the user's ID. This field is set to record the user's login ID.
Public static string key = "";
Public static string date = "";
Public static string bdate = "";
Public static string edate = "";
Public static int result_flag = 0;
Public static int number = 0;


// The following is an example of getting a template page.
Public static string muser_id = "";
Public static string muser_name = "";
Public static string mdegree = "";
Public static string mdepartment = "";

// View Details
Public static int result_number = 0;
Public static int result_whole = 0;

Public static sqlconnection con = new sqlconnection (SQL _con );
/// <Summary>
/// Define the SQL statement to be executed
/// </Summary>
Public static string sqlcmd = ""; // defines the SQL statement to be executed
Public static sqlcommand mycommand;
Public static sqldataadapter SDA = new sqldataadapter ();
Public static dataset DS = new dataset ();
# Endregion

/// <Summary>
/// This method is used to execute the SQL statement assigned to sqlcmd.
/// </Summary>
Public static void sqldc ()
{
Con. Close ();
If (con. State = connectionstate. Closed)
Con. open ();
Mycommand = new sqlcommand (sqlcmd, con );
If (sqlcmd. substring (0, 1) = "S" | sqlcmd. substring (0, 1) = "S ")
SDA. selectcommand = mycommand;
Else if (sqlcmd. substring (0, 1) = "D" | sqlcmd. substring (0, 1) = "D ")
SDA. deletecommand = mycommand;
Else if (sqlcmd. substring (0, 1) = "U" | sqlcmd. substring (0, 1) = "U ")
SDA. updatecommand = mycommand;
Else
SDA. insertcommand = mycommand;
Mycommand. executenonquery ();
// Sqldatareader SDR = mycommand. executereader ();
If (sqlcmd. substring (0, 1) = "S" | sqlcmd. substring (0, 1) = "S ")
{
DS. Tables. Clear ();
SDA. Fill (DS );
}
}
}
}
The two above are highlighted. One is the address of your local database, which can be seen when you open SQL server2005. The other is your database name.

After the bucket is added, the bucket WB must be included in the CS file.

Using WB;

Dataaccess. sqlcmd= "Insert into whole (patient_id, doctor_id, doing_date, doing_state) values ('" + values () + "', '" + txtdoctor_id.text.trim () + "', '"+ time +"', 0 )";
Dataaccess. sqldc ();

 

Thank the boss for providing technical support.

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.