与数据库的 SQL 连接重复
好的,现在我正在使用 SQL 数据库从不同的表中获取值...所以我建立连接并获取如下所示的值:
DataTable dt = new DataTable();
SqlConnection connection = new SqlConnection();
connection.ConnectionString = ConfigurationManager.ConnectionStrings["XYZConnectionString"].ConnectionString;
connection.Open();
SqlCommand sqlCmd = new SqlCommand("SELECT * FROM Machines", connection);
SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);
sqlCmd.Parameters.AddWithValue("@node", node);
sqlDa.Fill(dt);
connection.Close();
所以这是页面上的一个查询,我正在页面上调用许多其他查询。
那么我是否需要每次打开和关闭连接......???
另外,如果不是,这部分在所有人中都是常见的:
DataTable dt = new DataTable();
SqlConnection connection = new SqlConnection();
connection.ConnectionString = ConfigurationManager.ConnectionStrings["XYZConnectionString"].ConnectionString;
connection.Open();
我可以将它放在一个函数中并调用它吗?代码看起来会更干净... 我尝试这样做,但出现如下错误:
当前上下文中不存在连接。
有什么建议吗???
谢谢
ok now i am using the SQL database to get the values from different tables... so i make the connection and get the values like this:
DataTable dt = new DataTable();
SqlConnection connection = new SqlConnection();
connection.ConnectionString = ConfigurationManager.ConnectionStrings["XYZConnectionString"].ConnectionString;
connection.Open();
SqlCommand sqlCmd = new SqlCommand("SELECT * FROM Machines", connection);
SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);
sqlCmd.Parameters.AddWithValue("@node", node);
sqlDa.Fill(dt);
connection.Close();
so this is one query on the page and i am calling many other queries on the page.
So do i need to open and close the connection everytime...???
also if not this portion is common in all:
DataTable dt = new DataTable();
SqlConnection connection = new SqlConnection();
connection.ConnectionString = ConfigurationManager.ConnectionStrings["XYZConnectionString"].ConnectionString;
connection.Open();
can i like put it in one function and call it instead.. the code would look cleaner...
i tried doing that but i get errors like:
Connection does not exist in the current context.
any suggestions???
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我用“使用”。您可以在其中包含任意数量的查询。完成后,它将为您清理。
I use "using". You can include as many queries as you like inside. When complete it will clean up for you.
通常是的,您可以为多个行集建立单独的连接。
如果您可以使用联接来生成单个有意义的行集,那么这通常是在服务器端而不是在客户端执行的一件好事。
您可能还想考虑建立多个连接并使用异步功能,以便同时而不是按顺序对所有请求进行排队 - 有一个 看看这篇文章。
Typically yes, you make individual connections for multiple rowsets.
If you can use joins to produce a single meaningful rowset, that's typically a good thing to do on the server side instead of the client side.
You may also want to look at making multiple connections and using the async features in order to queue all your requests simultaneously instead of sequentially - have a look at this article.
不,只要您使用相同的数据库,您就不必每次都打开和关闭连接。 需要更改的是
您每次
sqlCommand 的 queryString。
就像 @durilai 所说,[using
] 很有用。Using
实际上具有比这更多的功能,但本质上它在代码周围放置了一个 try/catch 块,并在这种情况下调用 dispose 来关闭连接。任何需要打开/关闭的东西都可以与 using 一起使用,例如文本编写器或其他对象。
No you do not have to open and close the connection every time as long as you are using the same database. What you need to change is the
sqlCommand's queryString every time.
Like what @durilai said, [
using
] is useful.Using
actually has more functions than this, but essentially it puts a try/catch block around your code and calls dispose to close the connection in this case.Anything that needs open/close can be used with using, so things such as text writers, or other objects.