社区
VB
帖子详情
sqlconnection的close与dispose的区别?
chinaczg
2004-03-11 01:53:43
我想释放一个连接,请问close好还是dispose好?他们的区别是什么?
...全文
89
1
打赏
收藏
sqlconnection的close与dispose的区别?
我想释放一个连接,请问close好还是dispose好?他们的区别是什么?
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
1 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
Randy002
2004-03-17
打赏
举报
回复
可惜没人回答我的问题~~
asp.net
sqlconnection
con.
close
和con.
dispose
区别
con.
close
() 是关闭连接,实际上是把连接放回ado.net的连接池,并没有真正关闭,所以再次连接时只是把连接从池中拿出来用,速度很快。 con.
dispose
是用来释放对象的所在内存,相对于new
sqlconnection
(); 只用
dispose
是不能关闭connection的,两者不是一回事,只用
close
也不能释放它所占的内存。 conn.
dispose
() 是销毁连接,彻底关闭。 您可能感兴趣的文章:C#基础:
Dispose
()、
Close
()、Finalize()的
区别
详解探讨C#中
Dispose
方法与
Close
方法的
区别
ADO.NET连接池示例
ADO.NET链接对象(
SqlConnection
或者OracleConnection)默认情况都开启连接池(平时编程时可能会忽略)。当我们调用
Close
或者
Dispose
方法时,实际并不断开连接,而是把连接放回连接池,再次使用时候重连接池中取得空闲资源。因为打开和关闭数据库连接开销比较大,所以连接池对于与数据库链接资源的控制上,加快客户端程序的效率都有很大地帮助。 示例中会看到如何控制连接池个数,链接对象关闭连接放回链接池中,有其他连接对象打开连接时重连接池中取得空闲的对应连接。
c#数据库操作的3种典型用法
using System;using System.Collections.Generic;using System.Text;using System.Data;using System.Data.SqlClient; namespace DatabaseOperate{ class SqlOperateInfo { //Suppose your ServerName is "aa",DatabaseName is "bb",UserName is "cc", Password is "dd" private string
sqlConnection
Command = "Data Source=aa;Initial Catalog=bb;User ID=cc;Pwd=dd"; //This table contains two columns:KeywordID int not null,KeywordName varchar(100) not null private string dataTableName = "Basic_Keyword_Test"; private string storedProcedureName = "Sp_InertToBasic_Keyword_Test"; private string sqlSelectCommand = "Select KeywordID, KeywordName From Basic_Keyword_Test"; //sqlUpdateCommand could contain "insert" , "delete" , "update" operate private string sqlUpdateCommand = "Delete From Basic_Keyword_Test Where KeywordID = 1"; public void UseSqlReader() {
SqlConnection
sqlConnection
= new
SqlConnection
(
sqlConnection
Command); SqlCommand sqlCommand = new SqlCommand(); sqlCommand.CommandType = System.Data.CommandType.Text; sqlCommand.Connection =
sqlConnection
; sqlCommand.CommandText = sqlSelectCommand;
sqlConnection
.Open(); SqlDataReader sqlDataReader = sqlCommand.ExecuteReader(); while(sqlDataReader.Read()) { //Get KeywordID and KeywordName , You can do anything you like. Here I just output them. int keywordid = (int)sqlDataReader[0]; //the same as: int keywordid = (int)sqlDataReader["KeywordID"] string keywordName = (string)sqlDataReader[1]; //the same as: string keywordName = (int)sqlDataReader["KeywordName"] Console.WriteLine("KeywordID = " + keywordid + " , KeywordName = " + keywordName); } sqlDataReader.
Close
(); sqlCommand.
Dispose
();
sqlConnection
.
Close
(); } public void UseSqlStoredProcedure() {
SqlConnection
sqlConnection
= new
SqlConnection
(
sqlConnection
Command); SqlCommand sqlCommand = new SqlCommand(); sqlCommand.CommandType = CommandType.StoredProcedure; sqlCommand.Connection =
sqlConnection
; sqlCommand.CommandText = storedProcedureName;
sqlConnection
.Open(); sqlCommand.ExecuteNonQuery(); //you can use reader here,too.as long as you modify the sp and let it like select * from .... sqlCommand.
Dispose
();
sqlConnection
.
Close
(); } public void UseSqlDataSet() {
SqlConnection
sqlConnection
= new
SqlConnection
(
sqlConnection
Command); SqlCommand sqlCommand = new SqlCommand(); sqlCommand.CommandType = System.Data.CommandType.Text; sqlCommand.Connection =
sqlConnection
; sqlCommand.CommandText = sqlSelectCommand;
sqlConnection
.Open(); SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(); sqlDataAdapter.SelectCommand = sqlCommand; DataSet dataSet = new DataSet(); //sqlCommandBuilder is for update the dataset to database SqlCommandBuilder sqlCommandBuilder = new SqlCommandBuilder(sqlDataAdapter); sqlDataAdapter.Fill(dataSet, dataTableName); //Do something to dataset then you can update it to Database.Here I just add a row DataRow row = dataSet.Tables[0].NewRow(); row[0] = 10000; row[1] = "new row"; dataSet.Tables[0].Rows.Add(row); sqlDataAdapter.Update(dataSet, dataTableName); sqlCommand.
Dispose
(); sqlDataAdapter.
Dispose
();
sqlConnection
.
Close
(); } }}
C#源代码 学生成绩管理系统
C#源代码 学生成绩管理系统 #region 执行SQL语句,返回SqlDataReader /// /// 执行SQL语句,返回SqlDataReader /// /// 要执行的SQL语句 /// 返回SqlDataReader,需手工关闭连接 public SqlDataReader GetReader(string sql) {
SqlConnection
con=new
SqlConnection
(DataAccess.ConnectionString); SqlCommand cmd=new SqlCommand(sql,con); SqlDataReader dr=null; try { con.Open(); dr=cmd.ExecuteReader(CommandBehavior.
Close
Connection); } catch (Exception ex) { dr.
Close
(); con.
Dispose
(); cmd.
Dispose
(); throw new Exception(ex.ToString()); } return dr; } #endregion
DBHelper方便连接数据库 DBHelper
using System; using System.Collections; using System.Collections.Generic; using System.Text; using System.Data; using System.Data.SqlClient; using System.Configuration; namespace MyOfficeDAL { public static class DBHelper { private static
SqlConnection
connection; public static
SqlConnection
Connection { get { string connectionString = ConfigurationManager.ConnectionStrings["MyOfficeConnectionString"].ConnectionString; connection = new
SqlConnection
(connectionString); if (connection == null) { connection.Open(); } else if (connection.State == System.Data.ConnectionState.
Close
d) { connection.Open(); } else if (connection.State == System.Data.ConnectionState.Broken) { connection.
Close
(); connection.Open(); } return connection; } } public static int ExecuteCommand(string safeSql) { SqlCommand cmd = new SqlCommand(safeSql, Connection); int result = cmd.ExecuteNonQuery(); return result; } public static int ExecuteCommand(string sql, params SqlParameter[] values) { SqlCommand cmd = new SqlCommand(sql, Connection); cmd.Parameters.AddRange(values); return cmd.ExecuteNonQuery(); } public static string ReturnStringScalar(string safeSql) { SqlCommand cmd = new SqlCommand(safeSql, Connection); try { string result = cmd.ExecuteScalar().ToString(); return result; } catch (Exception ex) { return "0"; } connection.
Close
(); } public static int GetScalar(string safeSql) { SqlCommand cmd = new SqlCommand(safeSql, Connection); try { int result = Convert.ToInt32(cmd.ExecuteScalar()); return result; } catch (Exception e) { return 0; } connection.
Close
(); } public static int GetScalar(string sql, params SqlParameter[] values) { SqlCommand cmd = new SqlCommand(sql, Connection); cmd.Parameters.AddRange(values); int result = Convert.ToInt32(cmd.ExecuteScalar()); return result; connection.
Close
(); } public static SqlDataReader GetReader(string safeSql) { SqlCommand cmd = new SqlCommand(safeSql, Connection); SqlDataReader reader = cmd.ExecuteReader(); return reader; reader.
Close
(); reader.
Dispose
(); } public static SqlDataReader GetReader(string sql, params SqlParameter[] values) { SqlCommand cmd = new SqlCommand(sql, Connection); cmd.Parameters.AddRange(values); SqlDataReader reader = cmd.ExecuteReader(); return reader; reader.
Close
(); reader.
Dispose
(); } public static DataTable GetDataSet(string safeSql) { DataSet ds = new DataSet(); SqlCommand cmd = new SqlCommand(safeSql, Connection); SqlDataAdapter da = new SqlDataAdapter(cmd); da.Fill(ds); connection.
Close
(); connection.
Dispose
(); return ds.Tables[0]; } public static DataTable GetDataSet(string sql, params SqlParameter[] values) { DataSet ds = new DataSet(); SqlCommand cmd = new SqlCommand(sql, Connection); cmd.Parameters.AddRange(values); SqlDataAdapter da = new SqlDataAdapter(cmd); da.Fill(ds); connection.
Close
(); connection.
Dispose
(); return ds.Tables[0]; } } }
VB
16,553
社区成员
110,527
社区内容
发帖
与我相关
我的任务
VB
VB技术相关讨论,主要为经典vb,即VB6.0
复制链接
扫一扫
分享
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章