Introduction to two methods for ASP. NET to read data from SQL Server

Source: Internet
Author: User

Use the data adapter sqldataadapter

Sqldataadapter ad = new sqldataadapter (); // create the connection object sqlconnection conn = new sqlconnection (); Conn. connectionstring = "Data Source =. \ sqlexpress; initial catalog = database; user id = username; Password = password "; // create the command object sqlcommand selectecmd = new sqlcommand (); selectecmd. commandtext = "select * from table;"; selectecmd. connection = conn; // set the selectcommand attribute ad of the data adapter. selectcommand = selectecmd; // create a DataSet object dataset datas = new dataset (); // use the data adapter to fill the dataset ad. fill (datas, "tables"); gridview1.datasource = datas. tables ["tables"]; gridview1.databind ();

Use sqldatareader

Sqlconnection con = new sqlconnection ("Data Source =. \ sqlexpress; initial catalog = database; user id = user; Password = password "); // sqlcommand cmd = new sqlcommand (); // CREATE command object cmd. connection = con; // connect to cmd. commandtext = sqlcmd; // configure the SQL statement cmd. parameters. add (New sqlparameter (parameter name, parameter value); con. open (); // open the connection sqldatareader DATA = cmd. executereader (commandbehavior. closeconnection); // The connection is automatically closed

Paste another sqlhelper. CS

Public static class sqlhelper {Private Static readonly string condb = configurationmanager. connectionstrings ["tuanweiappconn"]. connectionstring; // set the connection string public Enum sdacmd {select, delete, update, insert} // defines the enumerated type public static sqldatareader execreader (string sqlcmd, Params sqlparameter [] paralist) {try {sqlconnection con = new sqlconnection (condb); // sqlcommand cmd = new sqlcommand (); // create C Ommand object cmd. Connection = con; // use the connection cmd. commandtext = sqlcmd; // configure the SQL statement if (paralist! = NULL) {cmd. commandtype = commandtype. text; // configure command type foreach (sqlparameter para in paralist) {cmd. parameters. add (para);} // Add parameter} con. open (); // open the connection sqldatareader SDR = cmd. executereader (commandbehavior. closeconnection); Return SDR;} catch (exception) {Throw exception ;}} /// <summary> // The dataadapter method returns the dataset // </Summary> /// <Param name = "sqlcmd"> SQL statement </param> // /<Pa Ram name = "command"> Operation Parameter Enumeration type </param> // <returns> </returns> Public static dataset dataadapter (string sqlcmd, sdacmd command, // implement the adapter string tabname, Params sqlparameter [] paralist) {sqlconnection con = new sqlconnection (condb); // create the connection object sqlcommand cmd = new sqlcommand (); // CREATE command object cmd. connection = con; // use the connection object cmd. commandtext = sqlcmd; // configure the connection string if (paralist! = NULL) {cmd. commandtype = commandtype. text; // configure the command type foreach (sqlparameter para in paralist) // traverse the parameter {cmd. parameters. add (para) ;}// add parameter} sqldataadapter SDA = new sqldataadapter (); // create an adapter switch (command) // search condition {Case sdacmd. select: // execute SDA for select. selectcommand = cmd; break; Case sdacmd. insert: // execute SDA for insert. insertcommand = cmd; break; Case sdacmd. update: // If SDA is executed for update. updatecommand = cmd; break; Case sdacmd. delete: // execute SDA for Delete. deletecommand = cmd; break;} dataset DS = new dataset (); // create a dataset SDA. fill (DS, tabname); // fill in the dataset return Ds; // return the dataset }}

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.