【发布时间】: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])
);```
【问题讨论】:
-
我将从阅读 SqlClient 开始,特别是围绕 SQLCommand Execute 方法组。 docs.microsoft.com/en-us/dotnet/api/… 通常为插入执行 ExecuteNonQuery