0
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 的连接)

4

1 回答 1

2

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

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

最重要的是,您需要确定您的连接字符串和 SQL 服务器配置。

于 2013-03-12T16:24:28.583 回答