SQL database--data access

Source: Internet
Author: User

Data access:

corresponding namespaces:System.Data.SqlClient;

SqlConnection: Connection Object

SqlCommand: Command Object

SqlDataReader: Reader Object

Create a connection string

String connstring = "server=.; Database=mydb;user=sa;pwd=123 ";

Connecting Objects

SqlConnection conn = new SqlConnection (connstring);

To Create a Command object

SqlCommand cmd = conn. CreateCommand ();

write the SQL statement to execute

Cmd.commandtext = "SELECT * from Info";

Open Connection

Conn. Open ();

perform operation (read operation, return reader object)

SqlDataReader dr = cmd. ExecuteReader ();

Perform operation (add-and-revise operation, return number of rows)

Cmd. ExecuteNonQuery ();

working with Data

if (Dr. HasRows)--This is a Boolean-type value that returns True or false

{

Dr. Read ();

Console.WriteLine (Dr[0]);

Console.ReadLine ();

}

Else

{

Console.WriteLine (" Read failed! ");

}

Close Connection

Conn. Close ();

Dr. Read () is a database data access pointer that goes down one line each time it executes, returns true if there is content , and Dr Access is the current row data collection. You can use an index or column name to access the corresponding data

SQL database--data access

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.