. Net/c # connect to sqlserver,

Source: Internet
Author: User

. 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 ();

}

 

}

}

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.