读书人

数据库连接,该怎么处理

发布时间: 2013-01-04 10:04:13 作者: rapoo

数据库连接
程序里需要连接两个数据库、用时只连一个,可以随时在两个之间切换,我做了没什么效果、大神们帮我看吧、我把代码贴上来
这是切换数据库的后台代码

 protected void Button1_Click(object sender, EventArgs e)
{
if (CeShi())//这是测试下输入的是否正确
{
if (txtDatabaseName.Text.Trim()=="kj249")
{
try
{
string name=DbSqlHelper.kj249();//这个是dbsqlhelp帮助类里方法、返回连接字符串
if (name != null&&name!="")
{
Session["Name"] = "kj249";
lblShow.Text = string.Format("<span style=color:Red;font-size:20px>数据库切换成功</span>");
Response.Redirect("setup.aspx");
}
}
catch (Exception ex)
{
lblShow.Text = string.Format("<span style=color:Red;font-size:20px>数据库切换失败</span>");
lblSql.Text = ex.Message;
}
}
else
{
try
{
string name=DbSqlHelper.boram_web();
if (name != null && name != "")
{
Session["Name"] = "boram";
lblShow.Text = string.Format("<span style=color:Red;font-size:20px>数据库切换成功</span>");


Response.Redirect("index.aspx");
}
}
catch (Exception ex)
{
lblShow.Text = string.Format("<span style=color:Red;font-size:20px>数据库切换失败</span>");
lblSql.Text = ex.Message;
}
}


这是dbsqlhelp帮助类

public class DbSqlHelper
{
protected static string connectionString = ConfigurationManager.ConnectionStrings["boram_web"].ConnectionString;
public static string boram_web()
{
connectionString = ConfigurationManager.ConnectionStrings["boram_web"].ConnectionString;
return connectionString;
}
public static string kj249()
{
connectionString = ConfigurationManager.ConnectionStrings["kj249"].ConnectionString;
return connectionString;
}

这个是handler.ashx 不同的数据库加载不一样的菜单

public class Handler : IHttpHandler {
CollieryBll bll = new CollieryBll();
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
string name = context.Request["Name"];
if (name == null)
{
context.Response.Write(JsonTree().ToString());
}
else if (name == "kj249")
{
context.Response.Write(Tree249().ToString());
}
else
{
context.Response.Write(JsonTree().ToString());
}
}

不知道是这个方法根本行不通、还是说中间代码写的有问题、大家能帮我看下吗?谢谢、、、
------解决方案--------------------


建议楼主把帖子移到C#版块,问题或许能尽快解决
[解决办法]
config配置文件数据库连接字符串是怎样的。
[解决办法]
可以参考别人查询分析器。你选择不同的数据库后(COMBOBOX SELECTINDEX 改变事件)建立不同的连接
[解决办法]

引用:
这是config里的字符串、运行时也没有报什么错、就是什么都没有了、页面一刷新菜单都没有了

XML code


<configuration>
<connectionStrings>
<add name="boram_web" connectionString="Data Source=(local);Initial Catalog=BORAM_WEB;P……

加载菜单的那些应该放连接改变事件里面执行

读书人网 >.NET

热点推荐