当前位置: 代码迷 >> .NET新技术 >> 数据库连接解决办法
  详细解决方案

数据库连接解决办法

热度:92   发布时间:2016-04-25 01:41:49.0
数据库连接
程序里需要连接两个数据库、用时只连一个,可以随时在两个之间切换,我做了没什么效果、大神们帮我看吧、我把代码贴上来
这是切换数据库的后台代码
C# code
 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帮助类
C# code
 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 不同的数据库加载不一样的菜单
C# code
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里的字符串、运行时也没有报什么错、就是什么都没有了、页面一刷新菜单都没有了
  相关解决方案