【问题标题】:connection-string strange error连接字符串奇怪的错误
【发布时间】:2013-03-12 16:23:12
【问题描述】:
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)

【问题讨论】:

  • 您有访问 SQL Server 的权限吗?
  • 我已连接你是什么意思你有权限
  • 你说你已经连接了。怎么连接的?

标签: c# asp.net sql-server


【解决方案1】:

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

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

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

【讨论】:

  • 为什么集成安全几乎无法在生产环境中发挥作用?为什么要修复它?集成安全性被认为是进行数据库访问的最安全方式,这样凭据不会存储在面向 Web 的客户端上。
  • @MystereMan 请告诉我,在 ASP.NET 应用程序中,凭据何时会存储在客户端上?它们仅存储在服务器上的 web.config 中,这是一个私有的受限文件。应关闭/不使用集成安全性,以免错误地在服务器上使用实际上具有过多控制权的有效用户帐户。显式帐户应与显式凭据一起使用,至少可以减少人为错误。
  • 客户端是网络应用。它是数据库的客户端。关键是,如果服务器遭到入侵,那么攻击者现在可以访问数据库的凭据。
  • @MystereMan 如果服务器被入侵并且连接使用集成安全或显式密码,那么这两种情况都结束了。
  • 不一定。拥有用户名和密码提供了许多仅在不知道其凭据的情况下访问帐户的功能。例如,您现在可能能够猜出密码方案,或者您可能能够减少暴力破解过程。您可能拥有使用相同用户名和密码的其他类型的帐户。向攻击者提供比运行服务绝对必要的更多信息是一个坏主意。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-24
  • 1970-01-01
相关资源
最近更新 更多