如何将一个数据库表(用户)中的UserID从插入到另一个表(Ticket)?每个表中的两列都具有相同的数据类型(int)。请看一下:
用户
用户名
用户名
密码
名字
姓氏
电子邮件
更新
删除
票务
TicketID
dateCreated会
UserIDNum
FK
全名
电子邮件
主题
消息
删除
这些是所涉及的所有代码:
Partial Public Class mysupport
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Page.IsPostBack Then
MaintainScrollPositionOnPostBack = True
SetFocus(helpTopicDDL)
Else
SetFocus(fullNameTXTBOX)
End If
Dim sConnection As New SqlConnection(ConfigurationManager.ConnectionStrings("TrackTicketsConnectionString2").ConnectionString)
sConnection.Open()
If Session("Ticket") Is Nothing Then
Response.Redirect("SignIn.aspx")
Else
Dim cmdS As String = "Select * from Users Where Deleted='N' AND Username=@Username"
Dim cmdCheckEmail As New SqlCommand(cmdS, sConnection)
Dim cmd As New Data.SqlClient.SqlParameter("@Username", Data.SqlDbType.VarChar)
cmdCheckEmail.Parameters.Add("@Username", SqlDbType.VarChar)
cmdCheckEmail.Parameters.Item("@Username").Value = Session("Ticket")
Dim obj As Object = cmdCheckEmail.ExecuteScalar()
If obj IsNot Nothing Then
mailLBL.Text = Convert.ToString(obj)
End If
End If
sConnection.Close()
End Sub
Protected Sub submitBTN_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles submitBTN.Click
Dim sdConnection As String = ConfigurationManager.AppSettings("TrackTicketsConnectionString2")
Dim iRowCount As Integer
Dim cmdInsertTicket As New Data.SqlClient.SqlCommand
Dim conticket As New Data.SqlClient.SqlConnection
conticket.ConnectionString = sdConnection
cmdInsertTicket.Connection = conticket
cmdInsertTicket.CommandText = "Insert Into Ticket " _
& "( DateCreated, FullName, Email, TicketType, Subject, Message, Deleted)" _
& "Values( @DateCreated, @FullName, @Email, @TicketType, @Subject, @Message, @Deleted)"
'Dim appUserName As New Data.SqlClient.SqlParameter("@UserName", Data.SqlDbType.NVarChar)
'cmdInsertTicket.Parameters.Add(appUserName)
'cmdInsertTicket.Parameters.Item("@UserName").Value = User.Identity.Name
Dim appDateCreated As New Data.SqlClient.SqlParameter("@DateCreated", Data.SqlDbType.DateTime)
cmdInsertTicket.Parameters.Add(appDateCreated)
cmdInsertTicket.Parameters.Item("@DateCreated").Value = Now()
Dim appFullName As New Data.SqlClient.SqlParameter("@FullName", Data.SqlDbType.VarChar)
cmdInsertTicket.Parameters.Add(appFullName)
cmdInsertTicket.Parameters.Item("@FullName").Value = fullNameTXTBOX.Text
Dim appEmail As New Data.SqlClient.SqlParameter("@Email", Data.SqlDbType.VarChar)
cmdInsertTicket.Parameters.Add(appEmail)
cmdInsertTicket.Parameters.Item("@Email").Value = emailAddTXTBOX.Text
Dim appTicketType As New Data.SqlClient.SqlParameter("@TicketType", Data.SqlDbType.VarChar)
cmdInsertTicket.Parameters.Add(appTicketType)
cmdInsertTicket.Parameters.Item("@TicketType").Value = helpTopicDDL.SelectedValue
Dim appSubject As New Data.SqlClient.SqlParameter("@Subject", Data.SqlDbType.VarChar)
cmdInsertTicket.Parameters.Add(appSubject)
cmdInsertTicket.Parameters.Item("@Subject").Value = subjectTXTBOX.Text
Dim appMessage As New Data.SqlClient.SqlParameter("@Message", Data.SqlDbType.VarChar)
cmdInsertTicket.Parameters.Add(appMessage)
cmdInsertTicket.Parameters.Item("@Message").Value = messageTXTBOX.Text
Dim appDeleted As New Data.SqlClient.SqlParameter("@Deleted", Data.SqlDbType.Char)
cmdInsertTicket.Parameters.Add(appDeleted)
cmdInsertTicket.Parameters.Item("@Deleted").Value = "N"
conticket.Open()
Try
iRowCount = cmdInsertTicket.ExecuteScalar()
statusLBL.Text = "Ticket has been submitted successfully."
Catch
statusLBL.Text = "Ticket has not been submitted. Please try again."
End Try
conticket.Close()
End Sub
我真正想要的是一个人的用户ID在登录后填写表格并提交后,存储在票证表中。我不知道如何从Users表中提取数据以插入Ticket表。我还在学习任何帮助。
答案 0 :(得分:0)
编辑:
首次添加记录时,将UserID
插入Tickets
表要求您可以访问UserID
值。然后,您需要在INSERT
语句中传递此值。
看起来我们首先需要检索UserId。由于您使用的是FormsAuthentication
,我们可以从User.Identity.Name
对象中检索UserName,并将其用作我们第一个查询中的值来检索UserId。
Dim cmdS As String = "Select [UserID] from Users Where Deleted='N' AND UserName=@UserName"
Dim cmdGetUserId As New SqlCommand(cmdS, sConnection)
Dim cmd As New Data.SqlClient.SqlParameter("@UserName", Data.SqlDbType.VarChar)
cmdGetUserId.Parameters.Add("@UserName", SqlDbType.VarChar)
cmdGetUserId.Parameters.Item("@UserName").Value = User.Identity.Name
Dim obj As Object = cmdGetUserId.ExecuteScalar
Dim myUserId As Integer = Integer.Parse(obj)
现在我们拥有当前用户的UserId值,我们可以修改INSERT
语句和参数。
cmdInsertTicket.Connection = conticket
cmdInsertTicket.CommandText = "INSERT INTO Ticket " _
& "(UserID, DateCreated, FullName, Email, TicketType, Subject, Message, Deleted)" _
& "Values(@UserID, @DateCreated, @FullName, @Email, @TicketType, @Subject, @Message, @Deleted)"
Dim appUserId As New Data.SqlClient.SqlParameter("@UserID", Data.SqlDbType.Int)
cmdInsertTicket.Parameters.Add(appUserId)
cmdInsertTicket.Parameters.Item("@UserID").Value = myUserId
Dim appDateCreated As New Data.SqlClient.SqlParameter("@DateCreated", Data.SqlDbType.DateTime)
cmdInsertTicket.Parameters.Add(appDateCreated)
cmdInsertTicket.Parameters.Item("@DateCreated").Value = Now()
...
Dim appDeleted As New Data.SqlClient.SqlParameter("@Deleted", Data.SqlDbType.Char)
cmdInsertTicket.Parameters.Add(appDeleted)
cmdInsertTicket.Parameters.Item("@Deleted").Value = "N"
一旦用户通过身份验证,您就可以通过User.Identity
对象访问身份验证信息。如果您经常需要访问它,可能还想考虑实现自定义IIdentity
类来存储UserID。这是一篇关于自定义身份验证的MSDN文章:http://msdn.microsoft.com/en-us/library/ms172766(v=vs.80).aspx
更新:
关于以下评论,您正在检索 UserID ,因为SqlCommand
正在使用ExecuteScalar
方法执行,该方法返回第一列的值第一排。我建议仔细查看SqlCommand
对象:http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.aspx以及MSDN上的这个ADO.NET入门:http://msdn.microsoft.com/en-us/library/e80y5yhx(v=vs.80).aspx