. Net/c # connect to sqlserver,
Webconfig code
<Configuration>
<Deleetask>
<Add key = "myconnect" value = "server =.; UID = sa; password = '1'; database = test"/>
</AppSettings>
</Configuration>
Database code
Using System;
Using System. Data;
Using System. Configuration;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Web. UI. WebControls. WebParts;
Using System. Web. UI. HtmlControls;
Using System. Data. SqlClient;
/// <Summary>
/// Summary of DB_class
/// </Summary>
Public class DB_class
{
SqlConnection myconn;
SqlCommand mycmd;
DataSet ds; // DataSet
SqlDataAdapter adapt;
Public DB_class ()
{
//
// TODO: add the constructor logic here
//
}
Public SqlConnection GetConnection ()
{
String mystr = ConfigurationManager. receivettings ["myconnect"]. ToString ();
SqlConnection myconn = new SqlConnection (mystr );
Return myconn;
}
Public int ExecNonQuery (string strSql)
{
Try
{
Myconn = GetConnection (); // connect to the database
Mycmd = new SqlCommand (); // initialize a SqlCommand Class Object
Mycmd. Connection = myconn;
Mycmd. CommandText = strSql;
If (mycmd. Connection. State! = ConnectionState. Open)
{
Mycmd. Connection. Open (); // Open the Connection to the database
}
Int a = mycmd. ExecuteNonQuery (); // execute the SQL operation and return the number of affected rows
Return;
}
Catch (Exception ex)
{
Throw new Exception (ex. Message, ex );
}
Finally
{
If (mycmd. Connection. State = ConnectionState. Open)
{// Disconnect and release resources
Mycmd. Connection. Close ();
Myconn. Dispose ();
Mycmd. Dispose ();
}
}
}
Public string ExecScalar (string strSql)
{
Try
{
Myconn = GetConnection (); // connect to the database
Mycmd = new SqlCommand (); // initialize a SqlCommand Class Object
Mycmd. Connection = myconn;
Mycmd. CommandText = strSql;
If (mycmd. Connection. State! = ConnectionState. Open)
{
Mycmd. Connection. Open (); // Open the Connection to the database
}
// Use the ExecuteScalar method of the SqlCommand object to return the value in the first column of the First row
// StrSql = Convert. ToString (mycmd. ExecuteScalar ());
String another = Convert. ToString (mycmd. ExecuteScalar ());
Return another;
}
Catch (Exception ex)
{
Throw new Exception (ex. Message, ex );
}
Finally
{
If (mycmd. Connection. State = ConnectionState. Open)
{// Disconnect and release resources
Myconn. Dispose ();
Mycmd. Connection. Close ();
Mycmd. Dispose ();
}
}
}
Public DataTable GetDataSet (string strSql, string TableName)
{
Ds = new DataSet ();
Try
{
Myconn = GetConnection (); // connect to the database
Adapt = new SqlDataAdapter (strSql, myconn); // instantiate the SqlDataAdapter Class Object
Adapt. Fill (ds, TableName); // Fill the dataset
Return ds. Tables [TableName]; // return the set of Tables in the DataSet.
}
Catch (Exception ex)
{
Throw new Exception (ex. Message, ex );
}
Finally
{// Disconnect and release resources
Myconn. Close ();
Adapt. Dispose ();
Ds. Dispose ();
Myconn. Dispose ();
}
}
}