大师们。进来帮我看看代码。关于数据库连接的问题。
我数据库也设置了允许远程连接和开启了服务。但是还是打不开数据库。我想肯定是代码出错了,现在的教材不能信。学生有求于各位老师了。
public partial class login : System.Web.UI.Page
{
     protected void Page_Load(object sender, EventArgs e)
     {
         if (!IsPostBack)
         {
             if (Request.Cookies["StuNo"] != null)
                 textUserName.Text = Request.Cookies["StuNo"].Value;
         }
     }
     protected void btnLogin_Click(object sender, EventArgs e)
     {
         SqlConnection cn = new SqlConnection();
         cn.ConnectionString = "Data Source=.;Initial Catalog=xk;User ID=sa;Password=123456";  //我很怀疑这段        SqlCommand cmd = new SqlCommand();
         cmd.Connection = cn;
         cmd.CommandText = "select * from Student where StuNo=@StuNo and pwd=@pwd";
         cmd.Parameters.Add("@StuNo",SqlDbType.Char,8).Value = textUserName.Text.Trim();
         cmd.Parameters.Add("@pwd",SqlDbType.Char,8).Value = textPassword.Text.Trim();
         cn.Open();
         SqlDataReader dr = cmd.ExecuteReader();
         if (dr.Read())
         {
             Response.Cookies["StuNo"].Value = textUserName.Text.Trim();
             Response.Cookies["Password"].Value = textPassword.Text.Trim();
             Response.Cookies["StuNo"].Expires = DateTime.Now.AddDays(int.Parse(dpExpires.SelectedValue));
             Response.Cookies["Passwod"].Expires = DateTime.Now.AddDays(int.Parse(dpExpires.SelectedValue));
             Session["StuName"] = dr["StuName"].ToString();
             Response.Redirect("chat.aspx");
         }
         else
         {
             Literal lit = new Literal();
             lit.Text = "<script language='javascript'>window.alert('登录失败')</script>";
             Page.Controls.Add(lit);
         }
         cn.Close();
     }             
}  
------解决方案--------------------
你先使用 SQL Server Management Studio 使用SQL验证方式去连接看是否能连上
你的数据库里面必须附加了xk数据库