日期:2014-05-19  浏览次数:20377 次

关于只修改web.config就能修改首页登录连接的问题!
protected   void   Button1_Click(object   sender,   EventArgs   e)
        {
                string   strConn   =   "server=Localhost;uid=sa;pwd=;database=dzmusic ";
                string   strCmd= "select   count(*)   from   admin   where   Username   = ' "+   TextBox1.Text   + " '   and   Password   =   ' "+   TextBox2.Text   + " ' ";
                SqlConnection   myConnection   =   new   SqlConnection();
                myConnection.ConnectionString=strConn;
                SqlCommand   myCommand   =   new   SqlCommand(strCmd,myConnection);
                myCommand.Connection.Open();

                int   flag=(int)myCommand.ExecuteScalar();
                myCommand.Connection.Close();
                myConnection.Close();
                if   (flag> 0)
                {      
                        Session[ "username "]=TextBox1.Text;
                        Response.Redirect( "default2.aspx ");
                }
                else
                {
                        Label1.Text= "对不起用户名和密码不符! ";
                        TextBox2.Text= " ";
                }
        }
这是登录的Button1_Click事件,我想只改web.config文件里的数据库连接就可以改登录的,是不是一定要定义一个数据库连接类才可以?
要怎么改?

------解决方案--------------------
改登陆????????
应该是说把连接数据库的串放到config里
如web.config
<appSettings>
<add key= "DefaultDBConnString " value= " "server=Localhost;uid=sa;pwd=;database=dzmusic ">
</appSettings>

代码只要如这样调就可以了
private string defaultDBConnString = System.Configuration.ConfigurationSettings.AppSettings[ "DefaultDBConnString "];

下回你数据库IP里如IP,PASSWORDT等改动时只要改web.config即可,不需要改应用程序

------解决方案--------------------
private string strConnection = System.Configuration.ConfigurationSettings.AppSettings[ "strConnection "].ToString();
或者
private string strConnection = ConfigurationManager.AppSettings[ "strConnection "].ToString();
------解决方案--------------------
private string strConnection = System.Configuration.ConfigurationSettings.AppSettings[ "strConnection "];
===============
函数内定义private成员变量?
把private去掉