如何从 html 按钮将数据插入数据库

How to INSERT data into database from html buttons

我有一个 ASP.NET 网站和一个表格。我希望用户将数据提交到表单中并将其插入到数据库中。我已经看到了这个问题,但它总是有一些不同之处,这意味着我无法使用它。我的HTML代码如下:

<div class="trucenter">
    <form runat="server">
        <label for="username" style="color: white;">*Username:</label>
        <input type="text" id="user" name="username">

        <label for="pwd" style="color: white;">*Password:</label>
        <input type="password" id="pwd" name="password">

        <label for="img" style="color: white;">Profile Picture:</label>
        <input type="file" id="pfp" name="profilepicture" accept="image/*" style="color: white;">

        <label for="Country" style="color: white;">*Country:</label>
        <input type="text" id="cntry" name="country">

        <label class="label" for="leaguechoose" style="color: white;">League:</label>
        <asp:DropDownList ID="cboLeague" runat="server" Width="150px">
            **Populate me please!**
        </asp:DropDownList>

        <div style="padding: 10px">
            <asp:Button ID="SubmitID" OnClick="SubmitID_Click" Text="Submit" runat="server" />
        </div>
    </form>
</div>

注意按钮使用 ASP 按钮,因为在后面的代码中,我有:

Protected Sub SubmitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SubmitID.Click
    Using conn As New SqlConnection(My.Settings.DATABASE)
        Using cmdSQL As New SqlCommand("INSERT command to be added")
            What to put here?
        End Using
    End Using
End Sub

我真的不确定该怎么做,或者我是否已经开始了。我需要做的是从输入和下拉菜单中收集所有数据,并使用 SQL INSERT 将其输入到我的数据库中。如果有人回答,只需为任何 SQL 或表格命名,因为我可以在之后自行排序。非常感谢任何建议,因为我对 visual basic 不是很熟悉。

编辑-我的 Table

CREATE TABLE [dbo].[Player] (
    [PlayerId]       INT           NOT NULL,
    [Username]       NVARCHAR (20) NOT NULL,
    [Password]       NVARCHAR (20) DEFAULT ((1234)) NOT NULL,
    [ProfilePicture] IMAGE         NULL,
    [Country]        NVARCHAR (20) NOT NULL,
    [LeagueId]       INT           NULL,
    [DateJoined]     DATE          NULL,
    PRIMARY KEY CLUSTERED ([PlayerId] ASC),
    CONSTRAINT [FK_Player_League] FOREIGN KEY ([LeagueId]) REFERENCES [dbo].[League] ([LeagueId])
);```

好的,你的代码存根看起来很不错。

有“几种方法可以做到这一点。事实上,如果你不是那么擅长写作 SQL,我会尝试 post 第二种方法。但是,根据你所拥有的far,那么方法是这样的:

但是,我们首先需要修复您的标记。对于文本框等内容,请使用 ASP.net 控件 - 而不是 HTML 控件。如前所述,您可以将它们拖放到网页上。此外,由于您使用的是 color = white,因此您不会在表单上看到标签(除非您有一些背景颜色。

因此,丢弃“输入”按钮,并用 asp.net 个文本框替换它们。

我们现在有了这个标记:

    <style>
        body {background-color: gray;} 
    </style>

    <div style="text-align:right;width:20%">
    <p>
        <label for="username" style="color: white;">*Username:</label>
        <asp:TextBox ID="user" runat="server"></asp:TextBox>
    </p>
    <p>
        <label for="pwd" style="color: white;margin-left:20px">*Password:</label>
        <asp:TextBox ID="pwd" runat="server"></asp:TextBox>
    </p>
    <p>
        <label for="img" style="color: white;">Profile Picture:</label>
        <asp:TextBox ID="pfp" runat="server"></asp:TextBox>
    </p>
    <p>
        <label for="Country" style="color: white;margin-left:20px">*Country:</label>
        <asp:TextBox ID="cntry" runat="server"></asp:TextBox>
    </p>
    <p>
        <label class="label" for="leaguechoose" style="color: white;">League:</label>
        <asp:DropDownList ID="cboLeague" runat="server" Width="150px">
        </asp:DropDownList>
    </p>
  </div>

    <div style="padding: 10px">
        <asp:Button ID="SubmitID" OnClick="SubmitID_Click" Text="Submit" runat="server" />
    </div>

现在看起来像这样:

所以,现在是代码。

   Protected Sub SubmitID_Click(sender As Object, e As EventArgs) Handles SubmitID.Click

        Using conn As New SqlConnection(My.Settings.TEST4)
            Dim strSQL As String =
                "INSERT INTO tblUsers (User, Password, PictureURL, Country,League " &
                " VALUES (@User, @Pass, @PicURL, @Country, @League)"

            Using cmdSQL As New SqlCommand(strSQL, conn)

                With cmdSQL.Parameters
                    .Add("@User", SqlDbType.NVarChar).Value = user.Text
                    .Add("@Pass", SqlDbType.NVarChar).Value = pwd.Text
                    .Add("@PicURL", SqlDbType.NVarChar).Value = pfp.Text
                    .Add("@Country", SqlDbType.NVarChar).Value = cntry.Text
                    ' do you want value, or text from cbo box?
                    ' .Add("@League", SqlDbType.NVarChar).Value = cboLeague.SelectedItem.Value
                    .Add("@League", SqlDbType.NVarChar).Value = cboLeague.SelectedItem.Text
                End With

                conn.Open()
                cmdSQL.ExecuteNonQuery()

            End Using
        End Using

    End Sub