且构网

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

如何从本地计算机连接数据库服务器?

更新时间:2022-12-24 22:52:29

首先在web.config / app.config中设置连接字符串



然后从那里读取connectionstring

sqlconnection con = new sqlconnection(来自config的connectionstring)
First set connectionstring in web.config/app.config

Then read connectionstring from there
sqlconnection con=new sqlconnection(connectionstring from config)


你好Kumar,



如果你有多个开发区域(DEV,UAT,PROD),那么在web.config中存储连接字符串是一个很好的做法。

这对你来说会更容易读取连接字符串并在代码隐藏文件后建立连接。



在Web.config文件中找到connnectionstring部分并重新以下代码的地方



Hi Kumar,

If you have more than one region for development(DEV,UAT,PROD) its a good practise to store connection string in web.config.
Which will be easier for you to read the connection string and establish the connection in code behind file.

In Web.config file find connnectionstring section and replace with below code

<connectionStrings>
<add name="localConnectionString" connectionString="SERVER=.\SQL2008;DataBase=StudentDatabase;UID=rk.prabakar;pwd=*****;" providerName="System.Data.SqlClient"/>
<add name="serverConnectionString" connectionString="Server=server\SQL2008;Database=StudentServerDatabase;User ID=sa;Password=*******;providerName="System.Data.SqlClient"/>
</connectionStrings>





在服务器端代码中,



In server side code,

con = new SqlConnection(ConfigurationManager.ConnectionStrings["localConnectionString"].ToString()); //Points to local database connection string
or
con = new SqlConnection(ConfigurationManager.ConnectionStrings["serverConnectionString"].ToString()); //Points to server database connection string



完成数据库后,可以建立连接,如con.open();



您开始执行命令。

因此连接到服务器数据库完全取决于连接字符串。



问候,

RK


Once you finalized with database, you can establish the connection like con.open();

And you start executing your command.
So connecting to server database is solely depends on the connection string.

Regards,
RK