ASP. NET

Source: Internet
Author: User

* Connection object usage: SqlConnection, SqlCommand, and SqlDataAdapter
* Data Access Method

1. Get data:

// Reference the two namespaces

Using System. Data. SqlClient;
Using System. Data;

// Initialize the connection object
SqlConnection conn = new SqlConnection ();
Conn. ConnectionString = "User ID = sa; Initial Catalog = DataBaseName; Data Source = (local); Password = 111111 ";

// Open the connection
If (conn. State = ConnectionState. Closed)
{
Conn. Open ();
}

// Initialization command
SqlCommand cmd = new SqlCommand ();
Cmd. Connection = conn;
Cmd. CommandType = CommandType. Text;
Cmd. CommandText = "SQL statement ";

// This operation is used to insert, update, and delete data. The number of affected rows is returned.
Int I = cmd. ExecuteNonQuery ();

If (I> 0) {MessageBox. Show ("operation successful ");}
// The operation that is used to query the maximum value and so on. Only one data record is returned, and the data in the first column in the first row is returned.

Object obj = cmd. ExecuteScalar ();

// If you want to obtain a data set, we often use a data adapter. $ fire $ network $ defense # collection
DataTable dt = new DataTable ();
SqlDataAdapter adapter = new SqlDataAdapter ();
Adapter. SelectCommand = cmd;
Adapter. Fill (dt );

2. Bind data to the Data Control

String str = "Data Source =.; Initial Catalog = GridView; User ID = sa; Password = 111111 ";

String SQL = "select * from UserName ";
SqlConnection conn = new SqlConnection (str );
// Conn. Open (); Use SqlDataAdapter (data adapter) without writing

// SqlCommand comm = new SqlCommand (SQL, conn );
// SqlDataAdapter dr = new SqlDataAdapter (comm );

SqlDataAdapter dr = new SqlDataAdapter (SQL, conn); // the above two sentences can be merged into this one

DataSet ds = new DataSet (); // create a DataSet;

Dr. Fill (ds); // Fill in strong datasets $ fire $ network $ defense # collection # Set

This. GridView1.DataSource = ds;
This. GridView1.DataBind (); // bind the data source to the control,
// Conn. Close (); Close the database connection
If (conn. State = ConnectionState. Open) // judge the database connection status and whether to connect
{
Conn. Close ();
}

3. Use SqlDataReader:

To create a SqlDataReader, you must call the ExecuteReader method of the SqlCommand object instead of using the constructor directly.

String str = "Data Source =.; Initial Catalog = GridView; User ID = sa; Password = 111111 ";
String SQL = "select * from UserName ";
SqlConnection conn = new SqlConnection (str );
Conn. Open ();
SqlCommand comm = new SqlCommand (SQL, conn );
DataSet ds = new DataSet ();
SqlDataReader dr = comm. ExecuteReader ();

If (dr. Read ())
{
// The following two types of data can be obtained:
// This. TextBox1.Text = dr. GetString (1 );
// This. TextBox2.Text = dr. GetInt32 (3). ToString ();

This. TextBox1.Text = dr. GetString (dr. GetOrdinal ("Name "));
This. TextBox2.Text = dr. GetInt32 (dr. GetOrdinal ("Age"). ToString ();
}

// Cyclic output

While (dr. Read ())
{
Response. Write (dr ["Name"]);
Response. Write (dr ["Age"]);
Response. Write ("<br/> ");
}
Dr. Close ();
If (conn. State = ConnectionState. Open)
{
Conn. Close ();
}

SqlDataReader: provides a method to read rows from the SQL Server database only into the stream.

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.