【问题标题】:How to INSERT data into database from html buttons如何从 html 按钮将数据插入数据库
【发布时间】:2022-01-04 11:43:54
【问题描述】:

我有一个 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 不是很熟悉。

编辑-我的表

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])
);```

【问题讨论】:

标签: html asp.net vb.net


【解决方案1】:

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

有“几种方法可以做到这一点。事实上,如果你不擅长编写 SQL,我会尝试发布第二种方法。但是,就你目前所拥有的而言,这就是方法:

但是,我们首先需要修复您拥有的标记。对于文本框等内容,请使用 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

【讨论】:

  • 这很好,我遇到的唯一问题是表有一个ID字段,似乎需要我指定ID。我将如何更改我的表格以使 ID 自动生成?我已将表格添加到问题的末尾。错误是Cannot insert the value NULL into column 'PlayerId', table 'C:\USERS\LCWBR\DESKTOP\COMPUTING PROJECT\ASPNET\REDBALLSPEEDRUNNING\REDBALLSPEEDRUNNING\APP_DATA\REDBALL.MDF.dbo.Player'; column does not allow nulls. INSERT fails.
  • 您必须重新创建该表才能将 PayerId 更改为自动编号。 SQL management studio 可以为您进行此更改,但一个简单的 ALTER table 命令无法将 PK 更改为自动编号 - 它必须以这种方式创建。所以这现在是一个数据库问题和问题 - 不是真正的代码之一。我建议你安装 sql management studio。您要么需要一套简单的“GUI”工具来管理数据库,要么删除并重新创建表。
猜你喜欢
  • 2013-10-17
  • 2015-03-30
  • 1970-01-01
  • 1970-01-01
  • 2019-06-06
  • 2020-03-31
  • 2022-01-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多