动态获取数据源值仍提供空值 [英] getting Data Source value dynamically still giving null value

查看:78
本文介绍了动态获取数据源值仍提供空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将asp.net2.0与c#
一起使用
我想动态获取数据源值,系统将从该数据源访问网站并连接到连接字符串中

但仍显示空值
您可以更正我的代码吗?

i am using asp.net2.0 with c#

i want to get dynamically the Data Source value from which system iam acessing the website and concatenate in connection String

but still showing null value
can you correct my code which helps me

string strcon = "Initial Catalog=POS;User ID=sa;Password=103";
   protected void Page_Load(object sender, EventArgs e)
   {
       
       if (Page.IsPostBack == false)
       {
           string[] parts = strcon.Split(';');
                      
           string dataSource = "";
           for (int i = 0; i < parts.Length; i++)
           {
               string part = parts[i].Trim();
               if (part.StartsWith("Data Source="))
               {
                   dataSource = part.Replace("Data Source=", "");
                   break;
               }
           }
       }
   }

推荐答案

这看起来像一场噩梦.页面加载事件将连接字符串混搭的部分是基本上无法使用代码的部分.您是否尝试单步执行代码?你甚至尝试阅读它吗?您的变量都不以"Data Source =开头,因此您的代码不执行任何操作.您应该进行一些谷歌搜索,并阅读有关如何使用调试器的信息.那么无论如何,您都应该考虑一下您认为该代码有什么用处.然后,您应该阅读n层开发.
This looks like a nightmare. The part where your page load event is mashing up a connection string, is the part at which your code is basically unusable. Have you tried stepping through your code ? Did you even try reading it ? None of your variable starts with "Data Source=", therefore your code does nothing. You should do some googling and read up on how to use the debugger. then you should think about what on earth you thought this code was good for, anyhow. Then you should read up on n-tiered development.


在连接字符串中,对于数据库名称,它可以是数据源,也可以是服务器.请同时检查两者.
In the connectionstring, For the database name it can be data source or it may also be server. please check for both.


这篇关于动态获取数据源值仍提供空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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