我正在使用下面的代碼在表中插入數據,並且我在cmd.executenonquery()上得到錯誤....異常被標記作爲SQL異常和底層消息說「字符串或二進制數據將被截斷,該語句已被終止。」使用vb.net在sql server數據庫中插入數據時出錯
Dim str As String ' defines str as a string variable
'takes insertion query as a string in str variable
str = "Insert into Instructors Values(@inst_id, @inst_name, @contact, @game, 'N/A', 'N/A', 'Available')"
'defines a new command which takes query string and connection string as parameters
Dim cmd As New SqlCommand(str, con)
' defines Instructor ID parameter and takes its value from the form
Dim prmInstID As New SqlParameter("@inst_id", SqlDbType.Int)
prmInstID.Value = TxtInstID.Text
' defines Instructor Name parameter and takes its value from the form
Dim prmInstName As New SqlParameter("@inst_name", SqlDbType.Char)
prmInstName.Value = TxtInstName.Text
' defines Contact parameter and takes its value from the form
Dim prmContact As New SqlParameter("@contact", SqlDbType.VarChar)
prmContact.Value = MskdTxtCntctno.Text
' defines Specialty Game parameter and takes its value from the form
Dim prmGame As New SqlParameter("@game", SqlDbType.VarChar)
prmGame.Value = Me.ComboBox1.SelectedItem
cmd.Parameters.Add(prmInstID)
cmd.Parameters.Add(prmInstName)
cmd.Parameters.Add(prmContact)
cmd.Parameters.Add(prmGame)
cmd.ExecuteNonQuery()
con.Close()
你用'TxtInstID.Text','TxtInstName.Text','MskdTxtCntctno.Text'和'Me.ComboBox1.SelectedItem'測試了什麼值? –