将表单字段中的数据插入SQL

时间:2017-10-06 12:12:23

标签: c# sql .net visual-studio

自从成为.NET的新手以来,我在将VS 2013与SQL连接时遇到了困难。

我已将它与数据库连接但现在我想知道如何在我的AdventureWork Databaze中从我页面中的文本框中插入数据。能帮到我吗,因为我真的没有得到它 您可以在下面找到我的一些代码:

<h1>Add in Database</h1>
    <div>
    Enter Name:  <asp:TextBox ID="TextBox3" runat="server" ></asp:TextBox>
   </br></br>
</div>
    Enter Surname:  <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
    </br></br>
    Gender:  <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
    <br />
    <br />
    <asp:Label ID="Label1" runat="server" ForeColor="#CC3300" Text="Label"></asp:Label>
    </br></br>
    <asp:Button ID="Button2" runat="server" Text="Submit" OnClick="Button1_Click" />
    &nbsp;&nbsp;&nbsp;
    <asp:SqlDataSource ID="SqlDataSource2" runat="server">
        ConnectionString="<$ ConnectionStrings:AdventureWorks2014ConnectionString >"
         SelectCommand="INSERT INTO [users] VALUE ([FirstName] = @FirstName)" ProviderName="System.Data.SqlClient">
    </asp:SqlDataSource>

    <br />
    <br />

    <br />
        </form>

和:`

 protected void Button2_Click(object sender, EventArgs e)
    {
        SqlConnection cnn = new SqlConnection();
        cnn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["AdventureWorks2014ConnectionString"].ConnectionString;
        cnn.Open();
        SqlCommand cmd = new SqlCommand("Insert  into users (FirstName,LastName,Gender) Values (@Name,@Surname,@Gender)",cnn);
        cmd.Parameters.AddWithValue("@Name", TextBox3.Text);
        cmd.Parameters.AddWithValue("@Surname", TextBox4.Text);
        cmd.Parameters.AddWithValue("@Gender", TextBox5.Text);
        cnn.Open();
        cmd.ExecuteNonQuery();
        cnn.Close();

        if (IsPostBack)
        {

            TextBox3.Text = "";
            TextBox4.Text = "";
            TextBox5.Text = "";
        }
    }  

1 个答案:

答案 0 :(得分:0)

虽然我不确定为什么上面的代码不起作用。我复制了这个并重写了按钮单击处理程序代码。以下是我的工作示例:

protected void Button2_Click(object sender, EventArgs e)
{
    var connStr = ConfigurationManager.ConnectionStrings["AdventureWorks2014ConnectionString"].ConnectionString;
    using (var cnn = new SqlConnection(connStr)) {
        cnn.Open();

        var cmd = cnn.CreateCommand();
        cmd.CommandText = "Insert into users (FirstName,LastName,Gender) Values (@Name,@Surname,@Gender)";
        cmd.Parameters.AddWithValue("@Name", TextBox3.Text);
        cmd.Parameters.AddWithValue("@Surname", TextBox4.Text);
        cmd.Parameters.AddWithValue("@Gender", TextBox5.Text);

        var affectedRows = cmd.ExecuteNonQuery();
        //  Do validation here.
        //  This should be 1, the number of rows inserted.
    }

    if (IsPostBack)
        TextBox3.Text = TextBox4.Text = TextBox5.Text = string.Empty;       
}

我注意到在您的示例中,您尝试两次打开数据库连接。您可以进行的其他更正是将SqlConnection包装在using语句中。以这种方式处理对象时,它将为您关闭连接。还要检查从insert语句返回的结果行数。这可能会为您提供一些关于未插入数据的线索。如果您使用的是SQL Server Developer Edition,则可以启动SQL事件探查器以查看从应用程序发送到SQL Server的结果T-SQL语句。我希望这有帮助。