打开与sql数据库的连接 [英] opening the connection with sql database

查看:123
本文介绍了打开与sql数据库的连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Windows应用程序通过c#构建数据库应用程序.

我有不同的表单,我想以所有形式打开相同的连接,而不是打开每种表单中的连接并以close
形式将其关闭
谢谢

I''m building a database application, through c# using windows application.

I have different forms and I want to open the same connection in all forms instead of opening the connection in each form and closing it with the form close

Thank you

推荐答案

您已经用错误的方式了!

SQL连接是非常稀缺的资源.通过在表单加载时打开连接并在表单关闭时关闭连接,您将拒绝其他进程-在某些情况下,这可能会阻止您自己的应用程序正常工作,并且您不知道为什么:在开发环境中,它可以正常工作"

需要时打开连接.完成后立即将其关闭,并尽快将其处理.
You are doing it the wrong way already!

SQL Connections are a very scarce resource. By opening the connection on form load and closing it on form close, you are denying it to other processes - this may prevent your own application from working, under some circumstances and you will have no idea why: "It works fine in the development environment"

Open the connection when you need it. Close it as soon as you have finished, and dispose of it as quickly as possible.


是的,如OriginalGriff,
所述
对于ADO.NET,我们基本上是在断开连接模式下工作的.因此,我们将需要使用的所有信息一次放入DataSet中,然后在那儿进行操作.最后,我们传递数据集以使用数据库进行更新.

因此,在任何形式之外创建一个静态类,并公开一些方法来获取DataTable/dataset或执行查询.当需要数据库中的数据时,请使用此接口,该接口将依次使用SqlConnection连接到数据库并返回正确的数据.

当您要更新时,将数据集发送到公开的静态方法,然后对其进行更新.

这样,您就不会保持连接,也可以从一个公共位置更新所有内容.我们称其为DataLayer. :thumbsup:
Yes, as stated by OriginalGriff,

in case of ADO.NET, we work basically in disconnected mode. So we take all the informations required for use at a time into DataSet and later manipulate there. Finally we pass the dataset to update itself with the database.

So Create a static class outside any form, and expose few methods to get DataTable/dataset or execute a query. When data is required from the database use this interface which will in turn connect to database using SqlConnection and return you the correct data.

When you want to update, send the dataset to the exposed static method, and update the same.

By this way you are not holding your connection and also update everything from a common place. We call it as DataLayer. :thumbsup:


好..您每次使用它们时都必须打开和关闭连接..但是,如果您想实现一次写入-在任何地方运行的样式..然后幸运的是.NET带有ref关键字来帮助您..so,您可以做的就是这个..

1.创建一个类并放入用于连接数据库的方法.
ok..you''d always have to open and close your connections everytime you use them..but if you want to implement the write-once-run-everywhere style..then fortunately .NET come''s with the ref keyword to help you..so what you can do is this..

1. create a class and put in a method for connecting to the database..example
.
.
static class DB
{
   public static void connectDB(ref SqlConnection conn, ref SqlCommand cmd)
   {
      conn = new SqlConnection();
      conn.ConnectionString="[input connection string here]";

      cmd.Connection=conn;
      cmd.CommandType=CommandType.StoredProcedure;
                           //Im assuming a stored procedure
   }
}



因此,您要做的就是在要使用它的类中调用此方法,它将为您完成以上所有操作,而无需每次都重新键入它们..

例如,如果我想在名为useHere的其他类中使用它,则可以这样做..



So all you just have to do is to call this method inside the classes you want to use it, and it will do all the above for you without you re-typing them everytime..

Example if I want to use it in a different class named useHere, I would do this..

.
.
class useHere
{
  /* First instantiate the connection and command objects because you''ll just refer to them using the ref keyword */

  SqlConnection conn;
  SqlCommand cmd;

  /* Now assuming I want to add data to a table */

  public void addData(ref conn, ref cmd)
  {
     DB.connectDB(conn,cmd);
     cmd.CommandText="[input stored proc. name here]";
     conn.Open();
     cmd.ExecuteNonQuery();
     if(conn.State == ConnectionState.Open)
       {
          conn.Close();
       }

     cmd.Dispose();
  }



而且你是游戏.. :)如果您愿意,请接受答案并投票!



and you''re game.. :) . If you like it, accept answer and VOTE!


这篇关于打开与sql数据库的连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆