如何在基于ASP.NET的Web中动态更改数据源名称 [英] How do I change data source name dynamically in ASP.NET web based

查看:62
本文介绍了如何在基于ASP.NET的Web中动态更改数据源名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发Web应用程序,我必须在实体框架中动态更改数据源。

我遇到的错误是

i am working on web application where i have to change data source dynamically in entity framework .
error i faced is

The network path was not found





我尝试了什么:





What I have tried:

SqlConnection con = null;
            var dbToUse = objser.Getip((ddcmpname.SelectedValue).ToString()).ToList();

            con = new SqlConnection(string.Format("server=.;database={0};", dbToUse));
 
            string yourConnection = ConfigurationManager.ConnectionStrings["Entities"].ConnectionString.Replace("dbname", dbToUse[0].Location);
            con = new SqlConnection(yourConnection);
con.open();

推荐答案

我不知道我是否跟着你,但是,

I don't know whether I follow you here or not, but,
// Why this? 
con = new SqlConnection(string.Format("server=.;database={0};", dbToUse));
 
string yourConnection = ConfigurationManager.ConnectionStrings["Entities"].ConnectionString.Replace("dbname", dbToUse[0].Location);

// When you have to do this! 
con = new SqlConnection(yourConnection);

将代码更改为此,

Change your code to this,

SqlConnection con = null;
var dbToUse = objser.Getip((ddcmpname.SelectedValue).ToString()).ToList();
 
string yourConnection = ConfigurationManager.ConnectionStrings["Entities"].ConnectionString.Replace("dbname", dbToUse[0].Location); // Assuming dbToUser[0] has a Location property
con = new SqlConnection(yourConnection);
con.open();

我删除前一个语句的原因是,它没有任何区别。您没有打开该对象类型的连接,但是您在此处建立连接。使用此代码,您只需确保路径正确。



如果路径不正确,我们无法提供帮助,因为我们不知道您的网络设置和配置。在这种情况下, SQL Server连接字符串 - ConnectionStrings.com [ ^ ],可以帮到你。

The reason I remove the previous statement was, that it does not make any difference. You are not opening the connection for that object type, you are however making a connection here. With this code, you just need to make sure your path is correct.

If the path is incorrect, we are unable to help because we do not know your networks settings and configurations. In this case, SQL Server connection strings - ConnectionStrings.com[^], would be able to help you out.


这篇关于如何在基于ASP.NET的Web中动态更改数据源名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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