简体   繁体   English

c#:一个连接到sql server的类

[英]c# : one class to connect to sql server

Hello there I hope you're having a great time. 你好,我希望你过得愉快。 I have a question And I will break it down into 3 points: 我有一个问题我会把它分解为3分:

1: create a class to connect to sql server the connection should be made using sql server authentication. 1:创建一个连接到sql server的类,应该使用sql server认证进行连接。 This class should contain several variables for connection parameters. 此类应包含连接参数的多个变量。

2: create a user form that shows the current connection parameters. 2:创建一个显示当前连接参数的用户表单。 And allow the user to update those parameters. 并允许用户更新这些参数。 In this form there should be a button to test the connect and another button to save the user changes to the connection parameters. 在这种形式中,应该有一个按钮来测试连接,另一个按钮用于保存用户对连接参数的更改。

3: how to share the connection, created by the class we made in point 1, between different forms in the application. 3:如何在应用程序中的不同表单之间共享由我们在第1点中创建的类创建的连接。 Without keeping too many open connections ideally only one connection should be open. 在没有保持太多开放连接的情况下,理想情况下只应打开一个连接。

I will add the code that can solve this problem I hope that you can help me refine it. 我将添加可以解决此问题的代码,我希望您可以帮助我改进它。

I am new to all of this. 我是所有这一切的新手。 Thank you all for help. 谢谢大家的帮助。

  1. already exists; 已经存在; SqlConnection and maybe SqlConnectionStringBuilder SqlConnectionSqlConnectionStringBuilder
  2. that kinda already exists, via the IDE, but last time I checked this was not a redistributable dll. 有点已经存在,通过IDE,但上次我检查这不是一个可再发行的DLL。 You could, however, simply hook a SqlConnectionStringBuilder to a PropertyGrid - or just write the UI from scratch 但是,您可以简单地将SqlConnectionStringBuilder挂钩到PropertyGrid - 或者只是从头开始编写UI
  3. even "only one connection should be open" is wrong, IMO - let the inbuilt connection pooling deal with that; 甚至“只有一个连接应该打开”是错误的,IMO - 让内置的连接池处理它; all you need is some configuration class with the connection string - and just deal with the connections as you need them, very locally - ie 你需要的只是一些带有连接字符串的配置类 - 只需要处理你需要的连接,非常本地 - 即

     using(var conn = new SqlConnection(Config.ConnectionString)) { conn.Open(); // NOT SHOWN: do a couple of related operations } // <== and here, it dies 

1 : go to MSDN website you'll find what you need : http://msdn.microsoft.com/fr-fr/library/system.data.sqlclient.sqlcommand.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-2 1:访问MSDN网站,您将找到所需内容: http//msdn.microsoft.com/fr-fr/library/system.data.sqlclient.sqlcommand.aspx?cs-save-lang = 1&cs-lang = CSHARP#代码片断-2-

private static void ReadOrderData(string connectionString)
{
    string queryString = 
        "SELECT OrderID, CustomerID FROM dbo.Orders;";
    using (SqlConnection connection = new SqlConnection(
               connectionString))
    {
        SqlCommand command = new SqlCommand(
            queryString, connection);
        connection.Open();
        SqlDataReader reader = command.ExecuteReader();
        try
        {
            while (reader.Read())
            {
                Console.WriteLine(String.Format("{0}, {1}",
                    reader[0], reader[1]));
            }
        }
        finally
        {
            // Always call Close when done reading.
            reader.Close();
        }
    }
}

2: look at your connection properties ( http://msdn.microsoft.com/en-us/library/System.Data.SqlClient.SqlConnection_properties.aspx ) and fill a listView or equivalent with it 2:查看你的连接属性( http://msdn.microsoft.com/en-us/library/System.Data.SqlClient.SqlConnection_properties.aspx )并用它填充listView或等价物

3: Use previous SqlConnection.Open() to deal with it 3:使用以前的SqlConnection.Open()来处理它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM