C # Database call class

Source: Internet
Author: User
Web. config file: <Deleetask>
<Add key = "connectionstring" value = "Server = waygoing-12345; uid = sa; Pwd = 111111; database = 111"/>
</Appsettings> Class file: // Conn

Using system;
Using system. collections;
Using system. componentmodel;
Using system. Data;
Using system. Data. sqlclient;
Using system. drawing;
Using system. Web;
Using system. Web. sessionstate;
Using system. Web. UI;
Using system. Web. UI. htmlcontrols;
Using system. Web. UI. webcontrols;

Namespace datamtest
{
/// <Summary>
/// Conn summary.
/// </Summary>
Public class Conn
{
Public conn ()
{
//
// Todo: add the constructor logic here
//
}
Public sqlconnection connstr; // Connection string
Public String getconnstr () // Obtain the connection string
{
String constr;
Constr = system. configuration. configurationsettings. deleetpipeline ["connectionstring"];
Return constr;
}
Public void open () // Open the database
{
String constr;
Constr = getconnstr ();
Connstr = new sqlconnection (constr );
Connstr. open ();
}
Public void close () // Close the database
{
Connstr. Dispose ();
Connstr. Close ();
}
Public void execsql (string SQL) // Execute SQL statements without return values, such as adding, modifying, and deleting
{
Open ();
Sqlcommand cmd = new sqlcommand (SQL, connstr );
Cmd. executenonquery ();
Close ();
}
Public dataset (string SQL) // Return the DataSet object
{
Open ();
Sqldataadapter rs = new sqldataadapter (SQL, connstr );
Dataset DS = new dataset ();
Rs. Fill (DS, "ds ");
Return Ds;
}
Public dataview (string SQL) // return the dataview object
{
Dataset DS = new dataset ();
DS = dataset (SQL );
Dataview DV = new dataview (Ds. Tables [0]);
Return DV;
}
Public sqldatareader datareader (string SQL) // returns the datareader object
{
Open ();
Sqlcommand cmd = new sqlcommand (SQL, connstr );
Sqldatareader DR = cmd. executereader ();
Return Dr;
}

}
}

Call file:

// Aspx File

Using system;
Using system. collections;
Using system. componentmodel;
Using system. Data;
Using system. Data. sqlclient;
Using system. drawing;
Using system. Web;
Using system. Web. sessionstate;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. htmlcontrols;

Namespace datamtest
{
/// <Summary>
/// Summary of webform1.
/// </Summary>
Public class webform1: system. Web. UI. Page
{
Protected system. Web. UI. webcontrols. DataGrid datagrid1;
Protected system. Web. UI. webcontrols. Label label1;
Protected system. Web. UI. webcontrols. textbox textbox1;
Protected system. Web. UI. webcontrols. textbox textbox2;
Protected system. Web. UI. webcontrols. textbox textbox3;
Protected system. Web. UI. webcontrols. textbox textbox4;
Protected system. Web. UI. webcontrols. textbox textbox5;
Protected system. Web. UI. webcontrols. Button button1;
Protected system. Web. UI. webcontrols. Button button2;
Protected system. Web. UI. webcontrols. textbox textbox6;
Protected system. Web. UI. webcontrols. Button button3;

Private void page_load (Object sender, system. eventargs E)
{
// Place the user here Code To initialize the page

If (! Ispostback)
{
BIND ();
}
}
Private void BIND ()
{
Conn myconn = new conn ();
// Call the dataset Function
Dataset DS = new dataset ();
String SQL = "select * From admininfo ";
DS = myconn. dataset (SQL );
Datagrid1.datasource = Ds. Tables ["ds"]. defaultview;
Datagrid1.databind ();
Myconn. Close ();
// Call the datareader Function
Label1.text = "";
Sqldatareader DR = myconn. datareader (SQL );
While (dr. Read ())
{
Label1.text + = Dr ["adminid"] + "" + Dr ["adminname"] + "" + Dr ["password"] + "" + Dr ["lastlogin"] + "" + Dr ["lastloginip"] + "<br> ";
}
Myconn. Close ();
}

# Code generated by region web Form Designer
Override protected void oninit (eventargs E)
{
//
// Codegen: This call is required by the ASP. NET web form designer.
//
Initializecomponent ();
Base. oninit (E );
}

/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void initializecomponent ()
{
This. button2.click + = new system. eventhandler (this. button2_click );
This. button1.click + = new system. eventhandler (this. button#click );
This. button3.click + = new system. eventhandler (this. button3_click );
This. Load + = new system. eventhandler (this. page_load );

}
# Endregion

Private void button#click (Object sender, system. eventargs E)
{
Conn myconn = new conn ();
String SQL = "insert into admininfo (adminid, adminname, password, lastlogin, lastloginip) values ('" + textbox5.text + "', '" + textbox4.text + "', '"+ textbox3.text +"', '2017-1-1 ', '2017. 168.0.1 ')";
Myconn.exe csql (SQL );
BIND ();
}

Private void button2_click (Object sender, system. eventargs E)
{
Conn myconn = new conn ();
String SQL = "Update admininfo SET Password = '" + textbox1.text + "'where id =" + textbox2.text;
Myconn.exe csql (SQL );
BIND ();
}

private void button3_click (Object sender, system. eventargs e)
{< br> conn myconn = new conn ();
string SQL = "delete admininfo where id =" + textbox6.text;
myconn.exe csql (SQL);
BIND ();
}< br>

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.