Asp. NET to access the local database using Ado.net

Source: Internet
Author: User

For the ASP.net control, I believe we all use very comfortable, but I feel the control is still a lot of defects, the first is that the transplant is not very good, the code is kingly, the following gives Ado.net code, as long as the file added to the Web site under the App_Code, and then modify a few values, You can easily access your local database.

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
{

<summary>
Access to database internal data
Important variables in this class are indicated
</summary>
public class DataAccess
{


        Public dataaccess ()
         {
           //
            //todo: Add constructor logic here
            //
       }

        #region     defining public variables
         public static string Sql_con = "Data source=felven-pc//sqlexpress;initial catalog=bbs;integrated security= True ';  //connection string
        public static string UserID = ';          //Considering the user's ID to judge the user's identity after logging in, this field is specially used to record the ID of the user login
         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;


Here's what you get from the template page
public static string muser_id = "";
public static string muser_name = "";
public static string mdegree = "";
public static string mdepartment = "";

View Details Pass Values
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 execute
</summary>
public static string sqlcmd = ""; Define the SQL statement to execute
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 Sqldo ()
{
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 top two red, one is the address of their own local database, which can be seen when you open the SQL server2005, the following is your database name.

Add a good call later, you need to include the WB in the CS file namespace

Using WB;

Dataaccess.sqlcmd = "INSERT into whole (patient_id,doctor_id,doing_date,doing_state) VALUES (' + droppatient_id.") Selectedvalue.trim () + "', '" + txtdoctor_id. Text.trim () + "', '" + Time + "', 0";
Dataaccess.sqldo ();

Thank you, 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.