回 帖 发 新 帖 刷新版面

主题:数据库连接不上,请大家帮忙看看.  越快越好.

我用的是visual studio 2005 数据库是sql server 2000标准版.
我写了一个注册页,通过注册把用户的信息提交到数据库里面,
我不知道是什么地方出的错.总是提示连接错误

注册代码
   <TABLE WIDTH=770 BORDER=0 align="center" CELLPADDING=0 CELLSPACING=0>
        <form id="form1" runat="server">
    <TR align="center"  >
        <TD COLSPAN=3  class="css" valign="top" style="width: 781px; height: 38px"> 
</TD>
  </TR>
    <TR>
        <td colspan="3" rowspan="2" style="width: 781px">
        <table border="1" align="center" cellspacing="0" class="css" style="width: 770px; height: 491px" bordercolor="#adbdce" bordercolordark="#d4d6e0" bordercolorlight="#000099">
  <tr align="center">
    <td height="40" colspan="2" style="background-color: #d4d6e0">
        会员注册</td>
  </tr>
  <tr>
    <td  align="right" style="width: 115px; height: 28px; background-color: #d4d6e0;">
        &nbsp;<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="Username"
            ErrorMessage="用户名不能为空">*</asp:RequiredFieldValidator>用户名:</td>
    <td style="width: 336px" >&nbsp;<asp:TextBox ID="Username" runat="server" MaxLength="20" Width="150px"></asp:TextBox>
        <asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="Username"
            ErrorMessage="用户名已经存在" OnServerValidate="CustomValidator1_ServerValidate">用户名已经存在</asp:CustomValidator><span style="height: 29px; color: #ff0066;"></span></td>
  </tr>
  <tr>
    <td align="right" style="width: 115px; height: 28px; background-color: #d4d6e0;">
        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="Userpwd"
            ErrorMessage="密码不能为空">*</asp:RequiredFieldValidator>
        &nbsp;密码:</td>
    <td style="height: 28px; width: 336px;">&nbsp;<asp:TextBox ID="Userpwd" runat="server" MaxLength="20" TextMode="Password"
            Width="150px"></asp:TextBox></td>
  </tr>
  <tr>
    <td align="right" style="width: 115px; height: 28px; background-color: #d4d6e0;">
        <asp:CompareValidator ID="CompareValidator1" runat="server" ControlToCompare="Userpwd"
            ControlToValidate="Userpwd1" Display="Dynamic" ErrorMessage="两次输入密码不一致">*</asp:CompareValidator>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="Userpwd1"
            Display="Dynamic" ErrorMessage="确认密码不能为空">*</asp:RequiredFieldValidator>
        &nbsp;确认密码:</td>
    <td style="height: 28px; width: 336px;">&nbsp;<asp:TextBox ID="Userpwd1" runat="server" MaxLength="20" TextMode="Password"
            Width="150px"></asp:TextBox></td>
  </tr>
  <tr>
    <td align="right" style="width: 115px; height: 28px; background-color: #d4d6e0;">
        <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="CompanyName"
            ErrorMessage="公司名称不能为空">*</asp:RequiredFieldValidator>&nbsp; 公司名称:</td>
    <td rowspan="" style="height: 29px; width: 336px;">&nbsp;<asp:TextBox ID="CompanyName" runat="server" MaxLength="50" Width="253px"></asp:TextBox></td>
  </tr>
  <tr>
    <td align="right" style="width: 115px; height: 28px; background-color: #d4d6e0;">
        &nbsp;用户类型:</td>
    <td style="height: 28px; width: 336px;">&nbsp;<asp:DropDownList ID="UserType" runat="server">
            <asp:ListItem>货主</asp:ListItem>
            <asp:ListItem>车主</asp:ListItem>
            <asp:ListItem>物流企业</asp:ListItem>
        </asp:DropDownList><span style="height: 29px; color: #ff0066;"></span></td>
  </tr>
  <tr>
    <td align="right" style="width: 115px; height: 28px; background-color: #d4d6e0;">
        <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ControlToValidate="Linkman"
            ErrorMessage="联系人不能为空">*</asp:RequiredFieldValidator>联系人:</td>
    <td style="width: 336px">&nbsp;<asp:TextBox ID="Linkman" runat="server" Width="80px"></asp:TextBox><span style="height: 29px; color: #ff0066;"></span></td>
  </tr>
  <tr>
    <td align="right" style="width: 115px; height: 28px; background-color: #d4d6e0;">
        <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="Tel"
            ErrorMessage="联系电话格式填写错误" ValidationExpression="(\d{3}-\d{8})|(\d{4}-\d{7})">*</asp:RegularExpressionValidator>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" ControlToValidate="Tel"
            Display="Dynamic" ErrorMessage="联系电话不能为空">*</asp:RequiredFieldValidator>联系电话:</td>
    <td colspan="" style="width: 336px">&nbsp;<asp:TextBox ID="Tel" runat="server" Width="150px"></asp:TextBox><span style="height: 29px; color: #ff0066;"></span></td>
  </tr>
  <tr>
    <td align="right" style="width: 115px; height: 28px; background-color: #d4d6e0;">
        <asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" ControlToValidate="MobileTelephone"
            Display="Dynamic" ErrorMessage="手机号码格式填写错误" ValidationExpression="\d{11}">*</asp:RegularExpressionValidator>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server" ControlToValidate="MobileTelephone"
            Display="Dynamic" ErrorMessage="手机号码不能为空">*</asp:RequiredFieldValidator>
        &nbsp;手机:</td>
    <td style="height: 25px; width: 336px;">&nbsp;<asp:TextBox ID="MobileTelephone" runat="server"></asp:TextBox><span style="height: 29px; color: #ff0066;"></span></td>
  </tr>
  <tr>
    <td align="right" style="width: 115px; height: 28px; background-color: #d4d6e0;">
        <asp:RegularExpressionValidator ID="RegularExpressionValidator3" runat="server" Display="Dynamic"
            ErrorMessage="Email格式填写错误" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" ControlToValidate="Email">*</asp:RegularExpressionValidator>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator8" runat="server" ControlToValidate="Email"
            Display="Dynamic" ErrorMessage="Email不能为空">*</asp:RequiredFieldValidator>Email:</td>
    <td style="height: 32px; width: 336px;">&nbsp;<asp:TextBox ID="Email" runat="server"></asp:TextBox>
  </tr>
    <tr>
    <td align="right" style="width: 115px; height: 28px; background-color: #d4d6e0;">
        <asp:RequiredFieldValidator ID="RequiredFieldValidator9" runat="server" ControlToValidate="Address"
            ErrorMessage="联系地址不能为空">*</asp:RequiredFieldValidator>联系地址:</td>
    <td style="height: 32px; width: 336px;">&nbsp;<asp:TextBox ID="Address" runat="server" Width="250px"></asp:TextBox>
    </tr>
    <tr>
    <td align="right" style="width: 115px; height: 28px; background-color: #d4d6e0;">
        <asp:RegularExpressionValidator ID="RegularExpressionValidator4" runat="server" ControlToValidate="Postalcode"
            ErrorMessage="邮政编码格式填写错误" ValidationExpression="\d{6}">*</asp:RegularExpressionValidator>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator10" runat="server" ControlToValidate="Postalcode"
            Display="Dynamic" ErrorMessage="邮政编码不能为空">*</asp:RequiredFieldValidator>邮政编码:</td>
    <td style="height: 32px; width: 336px;">&nbsp;<asp:TextBox ID="Postalcode" runat="server"></asp:TextBox>
    </tr>
        <tr>
    <td align="right" style="width: 115px; height: 28px; background-color: #d4d6e0;">
        &nbsp;<asp:RequiredFieldValidator ID="RequiredFieldValidator11" runat="server" ControlToValidate="Problem"
            Display="Dynamic" ErrorMessage="提示问题不能为空">*</asp:RequiredFieldValidator>提示问题:</td>
    <td style="height: 32px; width: 336px;">&nbsp;<asp:TextBox ID="Problem" runat="server"></asp:TextBox>
            (取回密码时使用,请牢记)</tr>
        <tr>
    <td align="right" style="width: 115px; height: 28px; background-color: #d4d6e0;">
        &nbsp;<asp:RequiredFieldValidator ID="RequiredFieldValidator12" runat="server" ControlToValidate="Answer"
            Display="Dynamic" ErrorMessage="答案不能为空">*</asp:RequiredFieldValidator>问题答案:</td>
    <td style="height: 32px; width: 336px;">&nbsp;<asp:TextBox ID="Answer" runat="server"></asp:TextBox>
            &nbsp; &nbsp;(取回密码时使用,请牢记)</tr>
  <tr>
    <td align="right" style="width: 115px; height: 28px; background-color: #d4d6e0;">备注:</td>
    <td style="height: 89px; width: 336px;">&nbsp;<asp:TextBox ID="Bewrite" runat="server" Height="69px" TextMode="MultiLine"
            Width="315px" >无</asp:TextBox></td>
  </tr>
  <tr>
    <td colspan="2" align="center" style="background-color: #d4d6e0">&nbsp;<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text=" 注册 " />
        <input id="Button2" type="reset" value=" 重置 " />&nbsp;
        <asp:ValidationSummary ID="ValidationSummary1" runat="server" ShowMessageBox="True"
            ShowSummary="False" />
        <br />
    </td>
  </tr>
</table>
        </td>
    </TR>
    <TR>
    </TR>

    <TR>
        <TD COLSPAN=3 style="height: 45px; width: 781px;">
        </TD>
    </TR>
       </form>
</TABLE>


回复列表 (共6个回复)

沙发

.cs文件
 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 user : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (IsValid == true)
        {
            SqlConnection strcon = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["strcon"]);
            strcon.Open();
            string Username = this.Username.Text.ToString();
            string Userpwd = this.Userpwd.Text.ToString();
            string CompanyName = this.CompanyName.Text.ToString();
            string UserType = this.UserType.Text.ToString();
            string Linkman = this.Linkman.Text.ToString();
            string Tel = this.Tel.Text.ToString();
            string MobileTelephone = this.MobileTelephone.Text.ToString();
            string Email = this.Email.Text.ToString();
            string Address = this.Address.Text.ToString();
            string Postalcode = this.Postalcode.Text.ToString();
            string Bewrite = this.Bewrite.Text.ToString();
            string Problem = this.Problem.Text.ToString();
            string Answer = this.Answer.Text.ToString();
            string AddNow = Convert.ToString(DateTime.Now);
            SqlCommand scd1 = new SqlCommand("insert into tb_User (Username,Userpwd,CompanyName,UserType,Linkman,Tel,MobileTelephone,Email,Address,Postalcode,Bewrite,AddNow,Problem,Answer)values('" + Username + "','" + Userpwd + "','" + CompanyName + "','" + UserType + "','" + Linkman + "','" + Tel + "','" + MobileTelephone + "','" + Email + "','" + Address + "','" + Postalcode + "','" + Bewrite + "','" + AddNow + "','" + Problem + "','" + Answer + "')", strcon);
            scd1.ExecuteNonQuery();
            Response.Write("<script lanuage=javascript>alert('注册成功,请等待审核。');location='index.aspx'</script>");
        }
    }
    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        SqlConnection strcon = new SqlConnection(System.Configuration.
.AppSettings["strcon"]);
        strcon.Open();
        string username = this.Username.Text.ToString();
        SqlCommand cmd = new SqlCommand("select count(*) from tb_User where username='" + username + "'", strcon);
        int count = Convert.ToInt32(cmd.ExecuteScalar());
        if (count > 0)
        {
            args.IsValid = false;
        }
        else
        {
            args.IsValid = true;
        }
    }
}

板凳

最好是列出错误提示
你的数据连接打开了就没有关闭过

3 楼

“/wuliu”应用程序中的服务器错误。
--------------------------------------------------------------------------------

在建立与服务器的连接时出错。在连接到 SQL Server 2005 时,在默认的设置下 SQL Server 不允许进行远程连接可能会导致此失败。 (provider: 命名管道提供程序, error: 40 - 无法打开到 SQL Server 的连接) 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 

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

源错误: 


行 45:     {
行 46:         SqlConnection Strcon = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["Strcon"]);
行 47:        Strcon.Open();
行 48:         string username = this.Username.Text.ToString();
行 49:         SqlCommand cmd = new SqlCommand("select count(*) from tb_User where username='" + username + "'", Strcon);
 

源文件: e:\wuliu\User_send.aspx.cs    行: 47 


这是错误的信息

4 楼

这样的错误一般是服务器没有开启或者是你的连接数据库的字符串出错了,与程序代码没有关系,仔细检查一下吧.

5 楼

行 47:        Strcon.Open();
应该是你数据库连接的代码问题,检查下帐号 和驱动程序部分

6 楼

把异常抛出来看看

我来回复

您尚未登录,请登录后再回复。点此登录或注册