如何在桌面应用程序中使用连接字符串进行连接 [英] How to use connection string in desktop application for connectivity

查看:64
本文介绍了如何在桌面应用程序中使用连接字符串进行连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作桌面应用程序....我已经制作了表格,其中我制作了4个按钮ADD,SAVE,UPDATE,DELETE.我正在使用SQL SERVER 2008数据库.我能够将数据与SQL连接.现在的问题是对于每个按钮的编码,我不得不一次又一次地编写以下代码

i am making desktop application....i have made form inwhich i made 4 buttons ADD,SAVE,UPDATE,DELETE .I am using SQL SERVER 2008 database.i am able to connect data with SQL.Now the problem is for each button''s coding i have to write the following coding again and again

SqlConnection con = new SqlConnection("Data Source=SW-PC-20;Initial Catalog=PSM;Integrated Security=True");



当我将项目从一个系统复制到另一个系统时,我每次都要更改所有形式的数据源,这变得非常耗时........
我可以了解连接字符串的概念以及如何使用.
预先感谢
问候



and when i copy the project from one system to another,I have change the data source every time for all forms which become time consuming........
can i have concept of connection string and how to use.
thanx in advance
regards

推荐答案

您可以将连接字符串保存在应用程序设置 [^ ](请参阅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.


这篇关于如何在桌面应用程序中使用连接字符串进行连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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