繁体   English   中英

从asp.net表单将数据插入SQL表

[英]Inserting data into SQL table from asp.net form

我正在尝试建立一个注册Web表单,该表单将用户数据保存到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.Data.dll中发生类型'System.ArgumentException'的异常,但未在用户代码中处理

附加信息:初始化字符串的格式不符合从索引0开始的规范。

我不知道如何解决这个问题,但是非常感谢您的帮助。

你的问题在于

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();

例如,您可能想看一下这个问题: 如何设置SQL Server连接字符串?

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)密码存储

看起来您由用户键入的存储密码。 强烈建议存储密码的表示形式(某种哈希很容易从字符串中计算出来,但几乎不可能从哈希中检索出该字符串)。 可以在此处找到更多详细信息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM