连接字符串奇怪的错误

时间:2013-03-12 16:23:13

标签: c# asp.net 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;
using System.Data.SqlClient;
using System.Text;
using System.IO;
using System.Configuration;



namespace Iknowyourbrain
{

    public partial class WebForm1 : System.Web.UI.Page
    {
        public static void ClearControls(Control Parent)
        {

            if (Parent is TextBox)
            { (Parent as TextBox).Text = string.Empty; }
            else
            {
                foreach (Control c in Parent.Controls)
                    ClearControls(c);
            }
        }
        private void ExecuteInsert(string username, string password, string age, string gender, string emailaddress)
        {
            SqlConnection conn = new SqlConnection(GetConnectionString());
            string sql = "INSERT INTO tblRegistration (UserName, Password, Age, Gender, Email Address) VALUES "
                   + " (@UserName,@Password,@Age,@Gender,@Email Address)";

            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlParameter[] param = new SqlParameter[6];


                param[0] = new SqlParameter("@UserName", SqlDbType.VarChar, 50);
                param[1] = new SqlParameter("@Password", SqlDbType.VarChar, 50);
                param[2] = new SqlParameter("@Age", SqlDbType.Char, 10);
                param[3] = new SqlParameter("@Gender", SqlDbType.Int, 100);
                param[4] = new SqlParameter("@Email Address", SqlDbType.VarChar, 50);

                param[0].Value = username;
                param[1].Value = password;
                param[2].Value = age;
                param[3].Value = gender;
                param[4].Value = emailaddress;


                for (int i = 0; i < param.Length; i++)
                {
                    cmd.Parameters.Add(param[i]);
                }

                cmd.CommandType = CommandType.Text;
                cmd.ExecuteNonQuery();
            }
            catch (System.Data.SqlClient.SqlException ex)
            {
                string msg = "Insert Error:";
                msg += ex.Message;
                throw new Exception(msg);
            }
            finally
            {
                conn.Close();
            }



        }
        public string GetConnectionString()
        {
            //sets the connection string from your web config file "ConnString" is the name of your Connection String
            return System.Configuration.ConfigurationManager.ConnectionStrings["MyConsString"].ConnectionString;
        }

        protected void Page_Load(object sender, EventArgs e)
        {


        }

        protected void Button1_Click(object sender, EventArgs e)
        {

                //call the method to execute insert to the database
                ExecuteInsert(
                              TxtUserName.Text,
                              TxtPassword.Text,
                              DropDownListGender.SelectedItem.Text,
                              TxtAge.Text, TxtEmailAddress.Text);
                Response.Write("Record was successfully added!");
                ClearControls(Page);
            }




    }
}
到目前为止,

是我网站的代码。在我的web.config文件中,我有

<configuration>
    <system.web>
        <compilation debug="true" targetFramework="4.0" />
    </system.web>

  <connectionStrings>
    <add name="MyConsString" connectionString="Data Source=WPHVD185022-9O0;
                             Initial Catalog=MyDatabase;
                             Integrated Security=SSPI;"
                             providerName="System.Data.SqlClient" />
  </connectionStrings>
</configuration>

当我尝试进行测试以注册帐户时,我收到了此错误:

  

用户代码

未处理异常      

插入错误:发生了与网络相关或特定于实例的错误   同时建立与SQL Server的连接。服务器不是   发现或无法访问。验证实例名称是否正确   并且SQL Server配置为允许远程连接。

     

(提供商:命名管道提供商,错误:40 - 无法打开   连接到SQL Server)

1 个答案:

答案 0 :(得分:2)

无论出于何种原因,您的应用程序无法连接到连接字符串中指定的数据库。

这可能是一个糟糕的服务器或目录名称,坏的凭据(使用集成安全性几乎永远不会在生产环境中工作,如果它确实你应该解决这个问题),权限不足,帐户禁用等。它也可能是不允许远程连接到数据库,无论是一般情况还是从您所在的位置。

最重要的是,您需要确保连接字符串和SQL Server配置。