string myConnectString = ConfigurationSettings.AppSettings["ConnectionString"];
            
            SqlConnection myConnection = new SqlConnection(myConnectString );

解决方案 »

  1.   

    http://msdn.microsoft.com/zh-cn/library/system.data.sqlclient.sqldatareader.aspxmyConnectString地址没赋进去,SqlDataReader 用了记得关掉
      

  2.   

    我改好了,但是还是不行,感觉是Web.config配置的问题,这里面的连接字符串怎么写??
      

  3.   

    我改好了,但是还是不行,感觉是Web.config配置的问题,这里面的连接字符串怎么写??
      

  4.   

    我现在我大概知道是Web.config配置的连接字符串错了,那个Web.config里连接字符串要怎么写??,那格式能给我发下嘛吗。
      

  5.   

    我现在我大概知道是Web.config配置的连接字符串错了,那个Web.config里连接字符串要怎么写??,那格式能给我发下嘛吗。我现在打出来了,就是每次都是登陆失败,没有登陆成功,能不能看我看下代码
    在DAL中
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data.SqlClient;
    using System.Configuration ;
    using System.Data;
    namespace DAL
    {
        public class Class1DAL
        {
            public SqlDataReader show(string name)
            {
                //string c= System.Configuration.ConfigurationManager.AppSettings["ConnectionString"].ToString();
               // SqlConnection conn = new SqlConnection( c );
                string myConnectString = ConfigurationSettings.AppSettings["ConnectionString"];            SqlConnection myConnection = new SqlConnection(myConnectString);
                myConnection.Open();
                String sql = string.Format("select 编号 from 学生表 where 姓名='{0}'", name);
                SqlCommand cmd = new SqlCommand(sql, myConnection );
                return cmd.ExecuteReader();
     
            }
        }
    在 按控件butt1的代码using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using DAL;
    using System.Data.SqlClient;
    namespace WebApplication1
    {
        public partial class _Default : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {        }        protected void Button1_Click(object sender, EventArgs e)
            {
                Class1DAL dal=new Class1DAL();
                SqlDataReader dr = dal.show(TextBox1.Text);
                if (dr.Read())
                {
                    Response.Redirect("WebForm1.aspx");
                }
                else
                {
                    Response.Write("用户名与密码不一致");
                }        }
        }
    }
    最后是Web.config的代码 <appSettings>
            
            <add key="ConnectionString" value="Data Source=levev-PC;Initial Catalog=student;Integrated Security=True"></add>
            
          
      
        </appSettings>
    能不能看下错在哪
      

  6.   

    SqlDataReader 是每次用到的时候都要打开,然后及时关闭的 
    一般用
    using(SqlConnection myConnection = new SqlConnection( ))
    {
    ................................
    }
    比较方便,不过现在基本都用SqlDataTable了 比较方便管理,虽然比SqlDataReader 慢一些,几乎可以忽略。。
      

  7.   

     <appSettings>
           <add key="ConnectionString" value="Data Source=levev-PC;Initial Catalog=student;User ID=sa;password=sa;Integrated Security=False"/>
         </appSettings>        public SqlDataReader show(string name)
            {
                string myConnectString = ConfigurationSettings.AppSettings["ConnectionString"];
                SqlConnection myConnection = new SqlConnection(myConnectString);
                myConnection.Open();
                String sql = string.Format("select StationID from StationPar where Area ='{0}'", name);
                SqlCommand cmd = new SqlCommand(sql, myConnection);           
                return cmd.ExecuteReader();                    }
      

  8.   

    String sql = string.Format("select 编号 from 学生表 where 姓名='{0}'", name);
      

  9.   

    这是我改完的。你看下
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data.SqlClient;
    using System.Configuration ;
    using System.Data;
    namespace DAL
    {
        public class Class1DAL
        {
            public SqlDataReader show(string name)
            {
                string myConnectString = ConfigurationSettings.AppSettings["ConnectionString"];            SqlConnection myConnection = new SqlConnection(myConnectString);
                myConnection.Open();
                String sql = string.Format("select 编号 from 学生表 where 姓名='{0}'", name);
                SqlCommand cmd = new SqlCommand(sql, myConnection );
                return cmd.ExecuteReader();
     
            }
        }
    }
    然后是using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using DAL;
    using System.Data.SqlClient;
    namespace WebApplication1
    {
        public partial class _Default : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {        }        protected void Button1_Click(object sender, EventArgs e)
            {
                Class1DAL dal=new Class1DAL();
                Class1DAL da2=new Class1DAL();
                SqlDataReader dr = dal.show(TextBox1.Text);
                SqlDataReader da = da2.show(TextBox2.Text);
                if (da.Read ()==dr .Read ())
                {
                    Response.Redirect("1.aspx");
                }
                else
                {
                    Response.Write("用户名与密码不一致");
                }        }        protected void 取消_Click(object sender, EventArgs e)
            {
                TextBox1.Text = "";
                TextBox2.Text = "";
            }
        }
    }
    。为什么我有时候随便输入,都能登陆成功,有时候密码和账号都错了,还登陆成功。,哪里错了啊。、。。不知道怎么改
      

  10.   

    让他们返回字符串吧,通过返回的串进行比较,用这个   if (da.Read ()==dr .Read ())  来判断不行吧