0

如何从一个数据库表 (Users) 中获取要插入到另一表 (Ticket) 的 UserID?每个表中的两列都具有相同的数据类型 (int)。请看一下:

用户 用户
ID 用户

密码
名字
姓氏
电子邮件
更新
删除

Ticket
TicketID
DateCreated
UserIDNumFK
FullName
Email
Subject
Message
Deleted

这些是所有涉及的代码:

    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

我真正想要的是一个人的 UserID在他登录填写表单并提交后存储在Ticket表中。我不知道如何从 Users 表中提取数据以插入 Ticket 表。任何帮助都非常感谢,因为我还在学习。

4

1 回答 1

0

编辑:

UserID在首先添加记录时将插入Tickets表中需要您有权访问该UserID值。然后,您需要在INSERT语句中传递此值。

看起来我们首先需要检索 UserId。由于您正在使用FormsAuthentication,我们可以从对象中检索 UserName,User.Identity.Name并将其用作我们第一个查询中的值来检索 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

于 2013-06-05T18:06:12.713 回答