我有一個 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 或表命名,因為我可以在之后自己排序。任何建議都非常感謝,因為我對視覺基礎不是很熟悉。
編輯 - 我的表
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])
);```
uj5u.com熱心網友回復:
好的,您的代碼存根看起來很不錯。
有“幾種方法可以做到這一點。事實上,如果你不太擅長撰寫 SQL,我會嘗試發布第二種方法。但是,根據你目前的情況,這就是方法:
但是,我們首先需要修復您擁有的標記。對于文本框等內容,請使用 ASP.net 控制元件 - 而不是 HTML 控制元件。您可以如上所述將它們拖放到網頁上。此外,由于您使用的是顏色 = 白色,因此您不會在表單上看到您的標簽(除非您有一些背景顏色。
因此,轉儲“輸入”按鈕,并用 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
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/368460.html
