与数据库的 SQL 连接重复

发布于 2024-08-25 09:58:45 字数 970 浏览 2 评论 0原文

好的,现在我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

何时共饮酒 2024-09-01 09:58:45
  1. 您绝对可以共享“打开连接”代码,无需重复它。
  2. 使用 ASP.NET 的 SQL Server 提供程序,每次“关闭”连接的开销非常小。它只是将连接返回到进程的连接池(请参阅此处),因此打开下一个连接将使用很少的开销。我认为每次操作后关闭连接是一个很好的做法
  1. You can definitely share the "open connection" code, no reason to duplicate it.
  2. With the SQL Server provider for ASP.NET, there is very little overhead with "closing" the connection every time. It just returns the connection to your process' connection pool (see here), so opening the next connection will use very little overhead. I think it is good practice to close the connection after each operation
清醇 2024-09-01 09:58:45

我用“使用”。您可以在其中包含任意数量的查询。完成后,它将为您清理。

using (SqlConnection cn = new SqlConnection(connectionString))
{
    using (SqlCommand cm = new SqlCommand(commandString, cn))
    {
        cn.Open();
        cm.ExecuteNonQuery();
    }
}

I use "using". You can include as many queries as you like inside. When complete it will clean up for you.

using (SqlConnection cn = new SqlConnection(connectionString))
{
    using (SqlCommand cm = new SqlCommand(commandString, cn))
    {
        cn.Open();
        cm.ExecuteNonQuery();
    }
}
悲念泪 2024-09-01 09:58:45

通常是的,您可以为多个行集建立单独的连接。

如果您可以使用联接来生成单个有意义的行集,那么这通常是在服务器端而不是在客户端执行的一件好事。

您可能还想考虑建立多个连接并使用异步功能,以便同时而不是按顺序对所有请求进行排队 - 有一个 看看这篇文章

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.

以酷 2024-09-01 09:58:45

不,只要您使用相同的数据库,您就不必每次都打开和关闭连接。 需要更改的是

您每次

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文