码迷,mamicode.com
首页 > Windows程序 > 详细

C# USING ADO.NET

时间:2016-09-03 17:52:52      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:

作为一个初学者,内容摘自网络.

使用using,能在使用完之后,进行资源的自动释放。
例如:  SqlConnection conn = new SqlConnection(ConnStr);
            conn.Open();
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.ExecuteNonQuery();
            conn.Close();
            conn.Dispose();
与下面的语句是等效的:
using (SqlConnection conn = new SqlConnection(ConnStr))
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.ExecuteNonQuery();
            }
我们可以看出,在using语句中,省去了“conn.Close()”“conn.Dispose”操作,即无需手动编程关闭链接和释放资源。

using 语句允许程序员指定使用资源的对象应当何时释放资源。为 using 语句提供的对象必须实现IDisposable 接口。此接口提供了 Dispose 方法,该方法将释放此对象的资源可以有多个对象与 using 语句一起使用,但是必须在 using 语句内部声明这些对象。

可以在到达 using 语句的末尾时,或者在该语句结束之前引发了异常并且控制权离开语句块时,退出using 语句。

using语句的本质:

使用using语句实际上生成的IL代码中是一个try, finally代码块,在finally代码块里释放资源。

 

 

1.使用using 来对数据库进行操作,using是资源释放的一种缩写,用于实现了实现了IDisposable接口(释放对象资源的接口是IDisposable)

 

 private void button2_Click(object sender, RoutedEventArgs e)
        {
            //source 那边用点代表本机如果是其它机器你可以用ip地址,(本机也可以用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();//要先打开连接
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    //插入数据
                    cmd.CommandText = "insert into admin(name,password,rank) values(‘hello‘,‘123456‘,1)";
                    cmd.ExecuteNonQuery();
                }
                using (SqlCommand cmd = conn.CreateCommand()) 
                {
                    //参数的使用
                    cmd.CommandText = "select * from admin where rank=@rank";
                    cmd.Parameters.Add(new SqlParameter("@rank", 2));
                    //因为SqlDataReader实现了IDisposable接口,释放对象资源的接口是IDisposable
                    using (SqlDataReader reader = cmd.ExecuteReader()) 
                    {   
                        //查询到结果放在数据库中没有放到客户端中,以后可以用DataSet处理
                        while (reader.Read())
                        {
                            string name = (string)reader.GetString(0);
                            MessageBox.Show(name);
                        }
                    }
                    
                }
          }
}       

 

 

添加配置文件 :

在Winform下要新增App.config文件,在Asp.net下要新增web.config文件。

1.打开配置文件添加相关代码后如下即可:

技术分享
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add name="myconn" connectionString="Data Source=.;Initial Catalog=Northwind;
         Persist Security Info=True;User ID=sa;Password=110"/>
  </connectionStrings>
</configuration>
技术分享

2.添加using System.Configuration;并在项目上右键“添加引用”->.Net组件中"Using Configuration"

如果不在组件中添加引用的话,后续的代码编译不过。

 

 

3.如下为读取数据的相关代码

 

技术分享
 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.NET

标签:

原文地址:http://www.cnblogs.com/songlinux/p/5837392.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!