我想在连接数据库时创建两个对象,它运行良好吗? [英] I would like to create two object in connecting to the database, Does it work well?

查看:96
本文介绍了我想在连接数据库时创建两个对象,它运行良好吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在连接数据库时创建两个对象,它是否运行良好?



cnnString =" Data Source =" + datasource +" ;;初始目录=" + database1 +" ;;用户ID = + user_ID +" ;;密码=" + pwd

cnn1 =新的SqlConnection(cnnString)





cnnString =" Data Source =" ; + datasource +" ;;初始目录=" + database2 +" ;;用户ID = + user_ID +" ;;密码=" + pwd

cnn2 =新的SqlConnection(cnnString)

I would like to create two object in connecting to the database, Does it work well?

cnnString = "Data Source =" + datasource + "; Initial Catalog =" + database1 + "; User ID =" + user_ID + "; Password =" + pwd
cnn1 = New SqlConnection (cnnString)

and
cnnString = "Data Source =" + datasource + "; Initial Catalog =" + database2 + "; User ID =" + user_ID + "; Password =" + pwd
cnn2 = New SqlConnection (cnnString)

推荐答案

如果你问是否可以在两个不同的数据库创建两个连接同时,是的,你可以。



我问你是怎么做的,正如其他人已经指出的那样。
If you're asking if you can create two connections to two different databases at the same time, then yes, you can.

I question how you're doing it though, as someone else has already pointed out.


以下是如何将连接字符串存储在配置文件中并在代码中使用它:

Here is how you can store your connection string in the config file and use it in your code:
<add key="ConString" value="Data Source={0};Initial Catalog={1};User ID={2};Password={3}" />





在您的代码中:



In your code behind:

string connection = (ConfigurationManager.ConnectionStrings["ConString"].ConnectionString).ToString();

SqlConnection con = new SqlConnection(string.Format(connection, datasource, database1, user_ID, pwd));


这篇关于我想在连接数据库时创建两个对象,它运行良好吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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