2016-03-07 118 views
0

我正在嘗試構建一個註冊表單,它將用戶數據保存到SQL表中。這是我到目前爲止有:從asp.net插入數據到SQL表格

public static SqlConnection GetConnection() 
{ 
    String connection; 
    connection = @"example/file/path"; 

    return new SqlConnection(connection); 

} 
protected void submitButton_Click(object sender, EventArgs e) 
{ 
    SqlConnection myConnection = GetConnection(); 

    try 
    { 

     myConnection.Open(); 
     String myQuery = "INSERT INTO RegistrationDB([firstName], [lastName], [eMail], [dob], [userName], [password]) values ('" 
      +fNameBox.Text+ "' ,'"+ lNameBox.Text+"' ,'"+emailBox.Text+"' ,'" 
      + dobBox.Text+"', '"+userNameBox.Text+"' ,'"+passwordBox.Text+"';)"; 

     SqlCommand myCommand = new SqlCommand(myQuery, GetConnection()); 
     myCommand.ExecuteNonQuery(); 
     myConnection.Close(); 
    } 
    catch (Exception ex) 
    { 
     Response.Write(ex.Message); 
    } 
    finally 
    { 
     myConnection.Close(); 
    } 
} 

在我GetConnection()方法,我返回的連接時發生錯誤。我得到的錯誤是:

類型的異常「System.ArgumentException」發生在 System.Data.dll中,但在用戶代碼中沒有處理

附加信息:初始化字符串的格式不符合從索引0開始的規範。

我不知道如何克服這個問題,但任何幫助都非常感謝。

+0

'connection = @「example/file/path」;'?? –

+1

你有一個鳴笛偉大的SQL注入漏洞,使用參數查詢 –

+0

你將不得不提供一個真正的連接字符串 - 看到這裏http://www.connectionstrings.com/ – user1666620

回答

1

你的問題就出在

String connection; 
connection = @"example/file/path"; 
return new SqlConnection(connection); 

您的connectionString變量(在你的情況下連接)設置不正確,有多種方法可以做到這一點只是列出了最常見的2。

用戶名和密碼標準連接:

SqlConnection conn = new SqlConnection(); 
conn.ConnectionString = 
"Data Source=ServerName;" + 
"Initial Catalog=DataBaseName;" + 
"User id=UserName;" + 
"Password=Secret;"; 
conn.Open(); 

信任的連接:

SqlConnection conn = new SqlConnection(); 
conn.ConnectionString = 
"Data Source=ServerName;" + 
"Initial Catalog=DataBaseName;" + 
"Integrated Security=SSPI;"; 
conn.Open(); 

你可能想看看這個問題,例如: How to set SQL Server connection string?

0

Pijemcolu的回答是正確,但我認爲可以添加幾件事來增強您的代碼:

1)使用變量的專有名稱。例如: - 連接字符串是從實際的連接不同

public static SqlConnection GetConnection() 
{ 
    // if Windows Authentication is used, just get rid of user id and password and use Trusted_Connection=True; OR Integrated Security=SSPI; OR Integrated Security=true; 
    String connStr = "Data Source=ServerName;Initial Catalog=DataBaseName;User id=UserName;Password=Secret;"; 
    return new SqlConnection(connStr); 

} 

2)嘗試處置一次性對象(即實施IDisposable)應適當地設置。

另外,命令不應該使用字符串連接構造,而應該使用參數。在向查詢提供直接用戶輸入時,這一點尤爲重要,因爲惡意用戶可能會嘗試執行查詢來破壞數據(請閱讀有關SQL注入的更多信息)。

該連接只能在finally塊內關閉,因爲無論什麼情況都執行(在catch塊中引發異常)。

protected void submitButton_Click(object sender, EventArgs e) 
{ 
    SqlConnection myConnection = null; 
    try 
    { 
     using (myConnection = GetConnection()) 
     { 
      myConnection.Open(); 
      String myQuery = @" 
       INSERT INTO RegistrationDB([firstName], [lastName], [eMail], [dob], [userName], [password]) 
       values (@firstName, @lastName, @eMail, @dob, @userName, @password)"; 

      using (SqlCommand myCommand = new SqlCommand(myQuery, GetConnection()) 
      { 
       myCommand.Parameters.AddWithValue("@firstName", fNameBox.Text); 
       myCommand.Parameters.AddWithValue("@lastName", lNameBox.Text); 
       myCommand.Parameters.AddWithValue("@eMail", emailBox.Text); 
       myCommand.Parameters.AddWithValue("@dob", dobBox.Text); 
       myCommand.Parameters.AddWithValue("@userName", userNameBox.Text); 
       myCommand.Parameters.AddWithValue("@password", passwordBox.Text); 

       myCommand.ExecuteNonQuery(); 
      } 
     } 
    } 
    catch (Exception ex) 
    { 
     Response.Write(ex.Message); 
    } 
    finally 
    { 
     if (myConnection != null) 
      myConnection.Close(); 
    } 
} 

3)密碼存儲

它看起來像由用戶輸入您的密碼存儲。強烈建議存儲密碼的表示(某種散列很容易從字符串計算,但字符串幾乎不可能從散列中檢索)。更多細節可以在here找到。

+0

感謝您的幫助,但我得到了myCommand方法的錯誤。它表示它不存在於當前的情況下。我該怎麼辦? – Reggie

+0

@Reggie - 對不起,它被放在它的範圍之外。我已經編輯了答案,在適當的地方。 – Alexei