1
我有一個名爲WorkPerUser有以下的列表:插入查詢汽車索引主鍵
id int
username nchar(50)
RMANumber int
Charge real
「標識」是用1
我的代碼自動遞增背後的主鍵是:
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSubmit.Click
'Dim temp_id As String
'temp_id = Session("se_userid")
'SQL connection String and Query
Dim connectionString As String = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
Dim insertSql As String = "INSERT INTO [WorkPerUser] (id,username,RMANumber,Charge)" & " values (@id,@username,@rmanumber,@charge)"
'Create SQL connection
Dim con As New SqlConnection(connectionString)
'Create SQl command and parameters
Dim cmd As New SqlCommand()
cmd.Connection = con
cmd.CommandType = CommandType.Text
cmd.CommandText = insertSql
Dim id As New SqlParameter("@id", SqlDbType.Text)
id.Value =
cmd.Parameters.Add(id)
Dim username As New SqlParameter("@username", SqlDbType.NChar)
username.Value = Session("userName")
cmd.Parameters.Add(username)
Dim rmanumber As New SqlParameter("@rmanumber", SqlDbType.Int)
rmanumber.Value = txtRMA.Text.ToString()
cmd.Parameters.Add(rmanumber)
Dim charge As New SqlParameter("@charge", SqlDbType.Real)
charge.Value = txtCharge.Text.ToString()
cmd.Parameters.Add(charge)
Try
con.Open()
cmd.ExecuteNonQuery()
lblMsg.Text = "Job Inserted"
Catch ex As SqlException
Dim errorMessage As String = "Error registering job"
errorMessage += ex.Message
Throw New Exception(errorMessage)
Finally
con.Close()
End Try
End Sub
正如你可能已經注意到,我離開了id.Value =
空的,因爲我不知道發送給數據庫的內容。是否需要在查詢或其他地方更改任何內容?
謝謝。有效。還有一個問題。正如你所看到的,我註釋了temp_id = session(「se_userid」)的部分,並且我使用id.value = temp_id發送到數據庫。問題是所有的數據庫列「ID」越來越,是「000-000 ..」,而不是從aspnet_Users實際userId。有關於此的任何想法? – serge
因爲你不再插入Id列,你在做什麼?你想獲取當前登錄的用戶嗎?如果爲true,那應該是'temp_id = Membership.GetUser()。ProviderUserKey.ToString()'。如果是其他內容,請告訴我 – codingbiz
我試圖將由asp.net生成的實際用戶標識插入id列。當我檢查標籤上的輸出時,它是可以的,但是當我將它發送到數據庫中時,34個字符的guid僅爲零。 (我已經檢索了用戶標識並將其存儲在會話中) – serge