为什么一个连接必须是开放的nonquery但不填充数据集? [英] Why does a connection have to be open for a nonquery but not to fill a dataset?
问题描述
当我连接到我的C#应用程序SQL数据源,我可以使用下面的代码填充数据集。请注意,我不显式打开到数据源的连接。
SqlConnection的cw_con =新的SqlConnection(服务器=服务器;数据库=数据库;用户=用户;密码=密码);
的SqlCommand CMD =新的SqlCommand(SELECT * FROM例子,值=价值);
cmd.Connection = cw_con;
//创建DataSet的
的DataSet cw_ds =新的DataSet(cw_ds);
SqlDataAdapter的大=新的SqlDataAdapter();
da.SelectCommand = CMD;
//执行命令,并填写数据集
da.Fill(cw_ds);
cw_ds.Clear();
不过,如果我想执行一个nonquery如 INSERT INTO
或更新
为什么我必须使用显式打开连接 connection.Open();
SqlConnection的cw_con =新的SqlConnection(服务器=服务器;数据库=数据库;用户=用户;密码=密码) ;
cw_con.Open();
的SqlCommand CMD =新的SqlCommand(UPDATE例如设定值= value其中value =价值);
cmd.Connection = cw_con;
cmd.ExecuteNonQuery();
cw_con.Close();
这是因为的DataAdapter
时,它的最初关闭隐含打开连接。它会之后将其关闭,如果在此之前关闭,否则连接保持打开状态。
从的 MSDN :
Fill方法从检索数据使用SELECT
语句中的数据源。用选择指令
相关联的所述的IDbConnection对象必须是有效的,但它并不需要是开放的。如果填充被称为前的IDbConnection
为关闭时,它被打开来检索数据和
然后关闭。如果连接是打开填充被调用之前,
保持打开状态。
块引用>
在使用
的SqlCommand
你必须明确地打开和关闭连接自己
作为一个方面说明:
- 使用的参数,以避免SQL注入
- 使用的实施
的IDisposable
任何事情,特别是连接因为这将确保它得到处理使用语句 /尽快关闭,即使在发生错误的情况下
醇>When I connect to a SQL datasource in my C# application, I can fill a dataset using the following code. Note that I am not explictly opening a connection to the datasource.
SqlConnection cw_con = new SqlConnection("Server=Server;Database=Database;User=User;password=password"); SqlCommand cmd = new SqlCommand("SELECT * FROM Example WHERE value = value"); cmd.Connection = cw_con; //Create DataSet DataSet cw_ds = new DataSet("cw_ds"); SqlDataAdapter da = new SqlDataAdapter(); da.SelectCommand = cmd; //Execute Command and Fill DataSet da.Fill(cw_ds); cw_ds.Clear();
However, If I want to execute a nonquery such as a
INSERT INTO
orUPDATE
why do I have to explicitly open the connection by usingconnection.Open();
?SqlConnection cw_con = new SqlConnection("Server=Server;Database=Database;User=User;password=password"); cw_con.Open(); SqlCommand cmd = new SqlCommand("UPDATE example SET value = value WHERE value = value"); cmd.Connection = cw_con; cmd.ExecuteNonQuery(); cw_con.Close();
解决方案That is because a
DataAdapter
opens the connection implicitely when it's closed initially. It'll close it afterwards if it was closed before, otherwise the connection stays open.From MSDN:
The Fill method retrieves the data from the data source using a SELECT statement. The IDbConnection object associated with the select command must be valid, but it does not need to be open. If the IDbConnection is closed before Fill is called, it is opened to retrieve data and then closed. If the connection is open before Fill is called, it remains open.
When using a
SqlCommand
you must explicitely open and close connections yourself.As a side note:
- Use parameters to avoid SQL-Injection
- Use using-statement for anything implementing
IDisposable
, especially Connections since it ensures that it gets disposed/closed as soon as possible, even in the case of an error
这篇关于为什么一个连接必须是开放的nonquery但不填充数据集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!