且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在桌面应用程序中使用连接字符串进行连接

更新时间:2023-02-17 08:20:14

您可以将连接字符串保存在应用程序设置 [^ ](请参阅CP 如何在C#中使用设置类 [
You can save the connection string in Application Settings[^] (See CP How To Use the Settings Class in C#[^])

In your code you can the do:

// Properties.Settings.Default.DatabaseSettings - Contains your connection string.
SqlConnection con = new SqlConnection(Properties.Settings.Default.DatabaseSettings);



这样,您将一个连接字符串保存在文件"MyProgram.exe.config"中.



This way, you have one connection string saved in the file "MyProgram.exe.config".


您可以在app.config文件中使用连接字符串属性;

You can use the connections string property in app.config file;

<configuration>
         <connectionStrings>
        <add name="studentConnectionString" connectionString=" Data Source=SW-PC-20;Initial Catalog=PSM;Integrated Security=True;Integrated Security=True;  providerName="System.Data.SqlClient"/>
    </connectionStrings>
</configuration>



在后面的代码中,可以使用代码
调用此连接字符串.



In code behind, you can call this connection string using the code ,

SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["studentConnectionString"].ConnectionString);



之后,您只需要更改连接字符串部分.因为您仅使用名称进行引用.

希望你有这个主意.如果您需要更多说明,
请ping我.



After that you need to change the connection string part only. becouse you are refering using name only.

hop you got the idea. if you need more clarification,
please ping me.


查看 http://www.connectionstrings.com [ ^ ],详细说明如何将连接字符串与各种技术结合使用.
Check out http://www.connectionstrings.com[^] for detailed explanation on using connection strings with various technologies.