如何创建可用于所有sql server的连接类 [英] how to create a connection class which can be use for all sql server

查看:107
本文介绍了如何创建可用于所有sql server的连接类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,

我是Web开发的新手.
任何人都可以告诉我如何创建一个将接受多个SQl服务器名称的连接字符串的方法.该方法一旦创建便可以用于任何项目.
通过使用.cs文件或通过创建DLL.

并且所有其他方法将仅在此文件中声明,并且可以在任何项目中使用,我们可以访问所有方法

Dear all,

I am new to web development .
Can any one tell me how to create a connection string that will accept name of multiple SQl server name.means once created and can be use for any project.
either by using .cs file or BY creating DLL.

and all other method will be declared in this file only and can be use of any project and we can access all method

推荐答案

Keep connection string in the web config file

<connectionStrings>
       <add name="MainConnStr" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|main.mdf;User Instance=true" providerName="System.Data.SqlClient"/>
</connectionStrings>
in c# code
access it 
string connStr = ConfigurationManager.ConnectionStrings["MainConnStr"].ConnectionString;

In different project you just need to change the connection string in  web config


这篇关于如何创建可用于所有sql server的连接类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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