注册 登录
编程论坛 ASP.NET技术论坛

SQL2005连接问题

zore0 发布于 2008-10-02 10:41, 814 次点击
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class _login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string userName = Request.Form.Get("userName").ToString();
        string userPwd = Request.Form.Get("userPwd").ToString();
        SqlConnection con = new SqlConnection("server=.; database=login;uid=sa;pwd= ");
        con.Open();
        SqlCommand cmd =new SqlCommand ("select count(*) from login where userName='"+userName +" ' and userPwd='"+userPwd +" '",con);
        int count = Convert.ToInt32(cmd.ExecuteScalar());
        if (count > 0)
        {
            Response.Redirect("7_02.aspx");
        }
        else
        {
            Response.Redirect("7_03.aspx");
        }
    }
}
我在SQL server外围应用配置器里已经启用了允许远程连接服务,并启用sa用户,密码设为空,连接时为什么还会出现以下情况?

在建立与服务器的连接时出错。在连接到 SQL Server 2005 时,在默认的设置下 SQL Server 不允许进行远程连接可能会导致此失败。 (provider: 命名管道提供程序, error: 40 - 无法打开到 SQL Server 的连接)


是不是连接代码写错了,请各位帮帮忙,谢谢!

[[it] 本帖最后由 zore0 于 2008-10-2 16:54 编辑 [/it]]
5 回复
#2
chenlong2008-10-06 16:11
("server=.; database=login;uid=sa;pwd= ");
把“pwd=”后面的空格去掉试试,你的数据库名字是叫login吗?
#3
chenlong2008-10-06 16:15
"Data Source=.;database=login;uid=sa;pwd="
再不行就试试这个
#4
仰望星空2008-10-06 18:45
你确定你用SQL2005的时候能登陆成功?
#5
zore02008-10-06 19:42
把连接写成这样
 SqlConnection con = new SqlConnection(@"DataSource=localhost\SQLEXPRESS;database=login;uid=sa;pwd= ");
        con.Open();就可以了,这是为什么?
#6
linsion8812262008-10-06 21:52
应该写成("server=.; database=login;uid=sa;pwd= ;");
你写的是pwd=  应该是pwd=;
1