C#中Windows形式的连接字符串 [英] connection string in windows form in C#

查看:359
本文介绍了C#中Windows形式的连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 5至6个类,其中我正在Windows窗体中使用连接字符串应用程序,有什么方法只能声明一次并直接使用. 我们可以为此使用单例类吗??请举一些例子

i Have 5 to 6 classes in which i am using connection string in windows form application is there any method to declare it only once and use directly. can we use singleton class for this? please give some example

推荐答案

app.config中的utConnection字符串为
PutConnection string in app.config as
<configuration>
    <configsections>
    </configsections>
    <connectionstrings>
        <add name="dbConnection">
            connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\App_Data\HREChk.mdf;Integrated Security=True;User Instance=True"
            providerName="System.Data.SqlClient" />
    </add></connectionstrings>
</configuration>


并以
身份访问


and access this as

String connString = ConfigurationManager.ConnectionStrings["dbConnection"].ToString();


另请参见此示例
app.config中的连接字符串


See this Example also
Connection string in app.config


一种方法是为CONSTANTS创建一个文件,然后将所有常量移动到该文件中.该文件将包含一个静态类,并且所有应用程序范围内的常量都可以成为该类的公共静态成员.

做到这一点的另一种方法是在应用程序设置中使用它.项目属性->设置.在这里,您可以拥有连接字符串并按以下方式访问它:

One way to do that is to have a file for CONSTANTS and move all your constants in that file. This file will contain a static class and all the application wide constants can become public static members of this class.

Another way of doing this is to have it in application settings. Project properties->Settings. Here you can have the connectionstring and access it as:

Properties.Settings.Default.CONSTRING


将连接字符串添加到app.config文件&使用Rahul Rajat Singh解决方案的第二种方法.
Add your connection string in app.config file & use second method of Rahul Rajat Singh''s solution.


这篇关于C#中Windows形式的连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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