每次我尝试向数据库表添加信息时都会出现错误。
Error: An unhandled exception of type 'System.ArgumentException' occurred in System.Data.dll
Additional information: Input string was not in a correct format.Couldn't store <JHK> in KEY TAG Column. Expected type is Int32.
到目前为止,这是我的代码。
Imports System
Imports System.Data
Imports System.Data.OleDb
Public Class add_new_key_window
Private Sub TextBox4_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.Close()
End Sub
Private Sub add_new_key_window_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'saveRecord()
management_window.RKM_SystemTableAdapter.Insert(Me.TextBox1.Text, Me.TextBox2.Text, Me.TextBox3.Text, Me.TextBox4.Text, Me.TextBox5.Text, Me.TextBox6.Text, Me.RichTextBox1.Text)
management_window.RKM_SystemTableAdapter.Fill(management_window.RKM_System_dataDataSet.RKM_System)
clearTextboxes()
End Sub
'Public Sub saveRecord()
' Dim cn As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data source=" & Application.StartupPath & "\RKM System data.accdb")
' Dim str As String
' Try
' cn.Open()
' Dim cmd As New System.Data.OleDb.OleDbCommand("INSERT INTO [RKM System]([KEY TAG], LOCATION, [HOOK NO], [KEY TYPE], BRAND, SERIAL, [TAG COLOR], NOTES) " & _
' "VALUES ('" & TextBox1.Text & "','" & TextBox2.Text & "','1','" & TextBox3.Text & _
' "','" & TextBox4.Text & "','" & TextBox5.Text & "','" & TextBox6.Text & _
' "','" & RichTextBox1.Text & "') ", cn)
' str = cmd.ExecuteNonQuery
' MsgBox("The new record was inserted successfully!")
' Catch ex As Exception
' MsgBox("Error message: " & ex.Message)
' End Try
' cn.Close()
'End Sub
Public Sub clearTextboxes()
TextBox1.Clear()
TextBox2.Clear()
TextBox3.Clear()
TextBox4.Clear()
TextBox5.Clear()
TextBox6.Clear()
RichTextBox1.Clear()
End Sub
End Class
答案 0 :(得分:0)
您需要将textbox1.text中的值转换为整数 - 目前它要将字符串存储在int32字段中
例如,访问数据库可能具有类型为&#34; Number&#34;的字段。你需要把它放到哪一类。您试图直接将文本框的内容放入此字段,该字段不是数字,而是字符串。因此你需要&#34; Cast&#34;使用&#34; int.Parse(textbox1.Text)&#34;将文本框中的数据转换为正确的类型或类似的。
顺便说一句,在创建像这样的SQL查询时,绝不应该使用内联语句。请使用SQL参数,否则您将面临潜在的SQL注入攻击。
例如(在我的vb中c#不太好!):
var cmd = new System.Data.OleDb.OleDbCommand("INSERT INTO table1 (firstname, lastname) VALUES (@firstname, @lastname)");
cmd.Parameters.AddWithValue("@id", (int)textbox1.text);
cmd.Parameters.AddWithValue("@firstname", textbox2.text);
有关详细信息,请参阅
答案 1 :(得分:0)
Public Sub saveRecord()
Dim cn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data source=" &
myDbFileName)
Dim SQL as String = "INSERT INTO [RKM System]([KEY TAG], LOCATION, [HOOK NO],
[KEY TYPE], BRAND, SERIAL, [TAG COLOR], NOTES)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)"
' ? = a place holder for a parameter to be added later
Try
cn.Open()
Using cmd As New OleDBCommand(SQL, cn)
' add param values IN ORDER SPECIFIED
cmd.Parameters.AddWithValue("@p1", Convert.ToInt32(TextBox1.Text))
cmd.Parameters.AddWithValue("@p2", TextBox2.Text)
' etc
' number of AddWithValue lines must = number of "?" in SQL
cmd.ExecuteNonQuery
End Using ' disposes of the OLEDbCOmmand object
MessageBox.Show("The new record was inserted successfully!")
Catch ex As Exception
MessageBox.Show("Error message: " & ex.Message)
End Try
cn.Close()
End Sub
参数避免SQL注入攻击(阅读此内容)。
由于[KEY TAG]是DB中的整数列,而TextBox1.Text是字符串(总是),您需要使用Convert.ToInt32
来转换值(因为它是用户输入,您应该实际使用Integer.TryParse
如果他们输入&#34; red&#34;或&#34; ziggy&#34;)。
OleDB将命名参数视为简单占位符,因此这会让您遇到麻烦:
Dim SQL As String = "INSERT INTO Employee (FirstName, LastName)
Values (@FName, @LName)"
cmd.Parameters.AddWithValue("@LName", TextBox1.Text)
cmd.Parameters.AddWithValue("@FName", TextBox2.Text)
TextBox1将映射到FirstName,因为它是SQL中的第一个列名。