C # USING ADO

Source: Internet
Author: User
Tags finally block connectionstrings

As a beginner, the content is excerpted from the Web.

With using, the automatic release of resources can be done after use. For example: SqlConnection conn = new SqlConnection (CONNSTR);
Conn. Open ();
SqlCommand cmd = new SqlCommand (SQL, conn);
Cmd. ExecuteNonQuery ();
Conn. Close ();
Conn. Dispose (); is equivalent to the following statement:using (SqlConnection conn = new SqlConnection (CONNSTR))
{
Conn. Open ();
SqlCommand cmd = new SqlCommand (SQL, conn);
Cmd. ExecuteNonQuery ();
We can see that the "conn" is omitted from the using statement. Close () "" Conn. Dispose operation, which means that you do not have to manually program closing links and freeing resources.

The using statement allows the programmer to specify when an object that uses resources should release resources . The object provided for the using statement must implement the IDisposable interface. This interface provides the Dispose method, which frees the resources for this object . you can have more than one object to use with a using statement, but you must declare the objects inside the using statement.

You can exit the using statement when you reach the end of a using statement, or if an exception is thrown before the statement ends and control leaves the statement block.

The nature of the using statement:

The IL code that is actually generated with the using statement is a try, finally block of code that frees up resources in the finally block.

1. Use a using to manipulate the database, using a resource release abbreviation for implementing the IDisposable interface (the interface that frees the object resource is IDisposable)

private void Button2_Click (object sender, RoutedEventArgs e)
{
Source side with a point to represent the local machine if it is another machine you can use IP address, (this machine can also use 127.0.0.1)
using (SqlConnection conn = new SqlConnection (
"Data source=127.0.0.1;initial Catalog=orderdb; User Id=sa; password=123456789 "))
{
Conn. Open ();//To turn on the connection first
using (SqlCommand cmd = conn. CreateCommand ())
{
Inserting data
Cmd.commandtext = "INSERT into admin (name,password,rank) VALUES (' Hello ', ' 123456 ', 1)";
Cmd. ExecuteNonQuery ();
}
using (SqlCommand cmd = conn. CreateCommand ())
{
Use of parameters
Cmd.commandtext = "SELECT * from admin where [email protected]";
Cmd. Parameters.Add (New SqlParameter ("@rank", 2));
Because the SqlDataReader implements the IDisposable interface, the interface that frees the object resource is IDisposable
using (SqlDataReader reader = cmd. ExecuteReader ())
{
The query results are placed in the database and are not placed in the client, and can later be processed with a dataset
while (reader. Read ())
{
String name = (string) reader. GetString (0);
MessageBox.Show (name);
}
}

}
}
}

To add a configuration file:

To add the App. Config file under WinForm, add the Web. config file under ASP.

1. After opening the configuration file, add the relevant code as follows:

<?xml version= "1.0" encoding= "Utf-8"?><configuration>  <connectionStrings>    <add name= "MyConn" connectionstring= "Data source=.;i Nitial Catalog=northwind;         Persist Security info=true; User Id=sa; password=110 "/>  </connectionStrings></configuration>

2. Add a using System.Configuration, and right-click on the item "Add Reference"->. NET component "Using Configuration"

If you do not add references to the component, subsequent code compilation is not possible.

3. The following is the code to read the data

private void Button1_Click (object sender, EventArgs e)        {            string connstr = configurationmanager.connectionstrings["MyConn"]. ConnectionString;            using (SqlConnection conn = new SqlConnection (connstr))            {                SqlCommand comm = new SqlCommand ("Select FirstName from Employees ", conn);                Conn. Open ();                SqlDataReader SDR = Comm. ExecuteReader ();                                while (SDR. Read ())                {                    listBox1.Items.Add (sdr[0]. ToString ());}}}        

C # USING ADO

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.