2013-03-12 283 views
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> 

,當我試圖做一個測試,註冊一個賬號,我得到這個錯誤:

Exception was unhandled by user code

Insert Error:A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections.

(provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

+0

您是否有權限訪問SQL Server? – Melanie 2013-03-12 16:26:21

+0

即時聯繫你是什麼意思你有認知 – Boris 2013-03-12 16:33:23

+0

你說你已經連接。連接如何? – Melanie 2013-03-12 19:53:15

回答

2

不管什麼原因,你的應用程序無法連接到數據庫在連接字符串中指定。

這可能是一個糟糕的服務器或目錄名稱,錯誤的憑據(使用集成的安全性幾乎不會在生產環境中工作,如果它確實應該解決這個問題),權限不足,帳戶禁用等。不允許遠程連接到數據庫,無論是通常還是從您的位置。

底線,您需要確定您的連接字符串和SQL服務器配置。

+0

爲什麼集成安全幾乎從不在生產中工作?爲什麼它應該被修復?集成安全性被認爲是最安全的數據庫訪問方式,這種方式不會在面向客戶端的Web上存儲憑據。 – 2013-03-12 16:30:18

+0

@MystereMan在ASP.NET應用程序中,請告訴,何時將憑據存儲在客戶端上?它們只存儲在服務器上的web.config中,這是一個私人的,受限制的文件。應關閉/不使用集成安全性,以免錯誤地在服務器上使用有效的用戶帳戶,而這些用戶帳戶實際上擁有過多的控制權。明確的帳戶應該使用明確的憑據,至少減少人爲錯誤。 – 2013-03-12 16:41:58

+0

客戶端是Web應用程序。它是數據庫的客戶端。關鍵是如果服務器受到威脅,那麼攻擊者現在可以訪問數據庫的憑證。 – 2013-03-12 16:48:28