2016-03-07 105 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