当前位置:编程学习 > C#/ASP.NET >>

登录代码没错吧为什么提示

在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器。请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接。 (provider: 命名管道提供程序, error: 40 - 无法打开到 SQL Server 的连接) 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void LogButton_Click(object sender, EventArgs e)
    {
       if(txtuser.Text!="" && txtpwd.Text!="")      
       {
           string connsql = "Data Source=(local);Database=Expert; User id=sa; password=123456";//连接字符串 
           SqlConnection conn=new SqlConnection(connsql);    
           try 
           {
               conn.Open();   
           }   
           catch(Exception ex)  
           {    
               Response.Write(ex.Message);    
               return;  
           }
           string mysql = "select * from [User] where Id='" + txtuser.Text + "' and Pwd='" + txtpwd.Text + ""; 
           SqlCommand cmd=new SqlCommand(mysql,conn);    
           SqlDataReader dr=cmd.ExecuteReader();  
           if(dr.Read())   
           {
               Response.Redirect("../Default2.aspx");        
           }   
           else 
           {    
               Response.Write("对不起,您的用户名或密码不正确!");  
           }  
           conn.Close(); 
           }  
    }
} sql server exception object string --------------------编程问答-------------------- 没有找到数据库,尝试改一下连接字符串 --------------------编程问答-------------------- 吧连接字符串的local改成.在试试!!! --------------------编程问答-------------------- server=.;uid=sa;pwd=123456;database=Expert; --------------------编程问答-------------------- 肯定是连接字符串的问题
如果上面的方法还不行的话,那教你用GridView控件新建数据源的方式自动生成连接字符串,就用这个连接串肯定是可以的 --------------------编程问答--------------------
断点调试进  if(dr.Read())   了吗 --------------------编程问答-------------------- 确定数据库服务开了吗 --------------------编程问答-------------------- 断点调试一下,先确定问题出在哪 --------------------编程问答-------------------- 你这典型的没有连上数据库! --------------------编程问答-------------------- 链接字符串换成下面试试:
 string connsql = "Data Source=.;Database=Expert; User id=sa; password=123456";//连接字符串 
--------------------编程问答-------------------- string mysql = "select * from [User] where Id='" + txtuser.Text + "' and Pwd='" + txtpwd.Text + "'"; 小了一点
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,