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 }}