2014-01-22 33 views
-2

我正嘗試在Visual Basic 2012上使用C#創建一個註冊頁面。當我調試時出現0錯誤,但是當我嘗試註冊一個帳戶時,出現以下錯誤。爲什麼我在')'錯誤附近得到不正確的語法?

「附近有語法錯誤)'」

如果我嘗試用現有的用戶名創建一個帳戶,它說用戶名已經存在。所以我能夠連接到SQL服務器,但我不知道我錯了哪裏。

這個註冊頁面應該建立在我的數據庫DNMembership帳戶>表>帳戶

這裏是我的代碼我的工作。

{ 
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["RegDNMembershipConnectionString"].ConnectionString); 
    con.Open(); 
    string insCmd = "Insert into Accounts (AccountName, Passphrase, EmailAddress, FullName, Country)"; 
    SqlCommand insertUser = new SqlCommand(insCmd, con); 
    insertUser.Parameters.AddWithValue("@AccountName", TextBoxUN.Text); 
    insertUser.Parameters.AddWithValue("@Passphrase", TextBoxPass.Text); 
    insertUser.Parameters.AddWithValue("@EmailAddress", TextBoxEA.Text); 
    insertUser.Parameters.AddWithValue("@FullName", TextBoxFN.Text); 
    insertUser.Parameters.AddWithValue("@Country", DropDownListCountry.SelectedItem.ToString()); 

    try 
    { 
     insertUser.ExecuteNonQuery(); 
     con.Close(); 
     Response.Redirect("Login.aspx"); 
    } 
    catch(Exception er) 
    { 
     Response.Write("<b>Something Really Bad Happened... Please Try Again.< /br></b>"); 
     Response.Write(er.Message); 
} 

我做錯了什麼?

+1

你需要掌握SQL上市的回答。它只在運行時執行。對於字符串中的SQL語法,您不會得到任何編譯器錯誤。 – Francis

回答

8

看起來你忘了你的INSERT命令添加VALUES一部分。

VALUES

介紹了列表或數據值的列表將被插入。 必須有 對於column_list中每列一個數據值,如果指定,或者在 表。值列表必須括在括號內。

更改您的SQL查詢等;

string insCmd = @"Insert into Accounts (AccountName, Passphrase, EmailAddress, FullName, Country) 
       VALUES(@AccountName, @Passphrase, @EmailAddress, @FullName, @Country)"; 

並使用using statement處置您的SqlConnectionSqlCommand等;

using(SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["RegDNMembershipConnectionString"].ConnectionString)) 
{  
    using(SqlCommand insertUser = new...) 
    { 
    //Your code.. 
    } 
} 
+1

+1是第一個正確的答案 – Habib

3

你有沒有在你的SQL指定的任何參數,或VALUES部分 - 你說「我想要插入到這些領域......」,而不是什麼要插入。它應該是這樣的:

string insCmd = 
    "Insert into Accounts (AccountName, Passphrase, EmailAddress, FullName, Country) " 
+ "Values (@AccountName, @Passphrase, @EmailAddress, @FullName, @Country"); 
1

您需要更改SQL語句:

string insCmd = "Insert into Accounts (AccountName, Passphrase, EmailAddress, FullName, Country) VALUES (@AccountName,@Passphrase,@EmailAddress,@FullName,@Country)"; 
1

你缺少Insert語句的一部分

INSERT INTO table (col1, col2) VALUES (@col1, @col2) 

或者,如果要插入的所有值到爲了列它們在表

INSERT INTO table VALUES (@col1, @col2) 
0

在SQL Server中有INSERT命令的幾種替代方法。

  • 指定COLUMNS,之後指定VALUES

    • SQL語法 - INSERT INTO TABLE(AccountName, Passphrase, EmailAddress, FullName, Country) VALUES ('AccountName', 'Passphrase', 'EmailAddress', 'FullName', 'Country')
    • C#string insCmd = "INSERT INTO TABLE(AccountName, Passphrase, EmailAddress, FullName, Country) VALUES (@AccountName, @Passphrase, @EmailAddress, @FullName, @Country)"
  • 如果你確信你可以跳過指定的列列的順序,這可能的情況下,是有風險的,你搞砸了的VALUES爲了您將值插入錯誤的列

    • SQL Sytanx - INSERT INTO TABLE VALUES ('AccountName', 'Passphrase', 'EmailAddress', 'FullName', 'Country')
    • C#string insCmd = "INSERT INTO TABLE VALUES (@AccountName, @Passphrase, @EmailAddress, @FullName, @Country)"

良好的資源,以讀取將

替代INSERT INTO TABLE您還可以從C#存儲過程插入進入表格。使用存儲過程可以幫助您減少臨時查詢,幫助防止SQL注入,減少網絡流量,添加額外的驗證服務器端。你的代碼如下所示。

SqlCommand cmd = new SqlCommand("usp_InsertIntoAccount", con); 
con.Open(); 

cmd.CommandType = CommandType.StoredProcedure; 

cmd.Parameters.Add(new SqlParameter("@AccountName", TextBoxUN.Text)); 
cmd.Parameters.Add(new SqlParameter("@Passphrase", TextBoxPass.Text)); 
cmd.Parameters.Add(new SqlParameter("@EmailAddress", TextBoxEA.Text)); 
cmd.Parameters.Add(new SqlParameter("@FullName", TextBoxFN.Text)); 
cmd.Parameters.Add(new SqlParameter("@Country", DropDownListCountry.SelectedItem.ToString())); 

try 
{ 
    cmd.ExecuteNonQuery(); 
    con.Close(); 
    Response.Redirect("Login.aspx"); 
} 
catch(Exception er) 
{ 
    Response.Write("<b>Something Really Bad Happened... Please Try Again.< /br></b>"); 
    Response.Write(er.Message); 
} 

其他資源在以下幾個問題How to execute a stored procedure within C# program

相關問題