如何解决溢出异常,而从Visual Basic 2010
连接到MS Access数据库我是新来的Visual Basic。我正在为我的迷你项目开发Visual Basic 2010中的一个项目。我想将以Visual Basic形式插入的数据存储到在ms access access 2007中创建的数据库中。我键入了下面的代码,但每次输入数值到窗体并按提交我在消息框中获得异常为“溢出”。我无法找出原因。请帮助我。如何解决溢出异常,而从Visual Basic 2010
以下是代码:
Imports System.Data.OleDb
Public Class dn_register
Dim provider As String
Dim dataFile As String
Dim connString As String
Dim myConnection As OleDbConnection = New OleDbConnection
Private Sub dn_sub_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dn_sub.Click
provider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="
dataFile = "F:\MyDatabase\MyProjectDatabase.accdb"
connString = provider & dataFile
myConnection.Close()
myConnection.ConnectionString = connString
myConnection.Open()
Dim str As String
str = "Insert into Dnr_tbl([Dname],[Age],[Bloodgroup],[Location],[Contact],[Email]) Values(?,?,?,?,?,?)"
Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
cmd.Parameters.Add(New OleDbParameter("Dname", CType(TextBox1.Text, String)))
cmd.Parameters.Add(New OleDbParameter("Age", CType(TextBox2.Text, Integer)))
cmd.Parameters.Add(New OleDbParameter("Bloodgroup", CType(TextBox3.Text, String)))
cmd.Parameters.Add(New OleDbParameter("Location", CType(TextBox4.Text, String)))
cmd.Parameters.Add(New OleDbParameter("Contact", CType(TextBox5.Text, String)))
cmd.Parameters.Add(New OleDbParameter("Email", CType(TextBox6.Text, String)))
Try
cmd.ExecuteNonQuery()
cmd.Dispose()
myConnection.Close()
TextBox1.Clear()
TextBox2.Clear()
TextBox3.Clear()
TextBox4.Clear()
TextBox5.Clear()
TextBox6.Clear()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
End Class
首先尝试缩小其参数引起的问题。您可以注释掉所有参数并将其添加回来,直到找到问题(根据需要调整SQL语句以匹配参数计数),或者选择可能的嫌疑犯并将其注释掉,并继续评论参数并调整SQL语句直到它工作。您可能需要暂时调整Access中的表格以允许测试列中的空值。整数值似乎是一个可能的候选人,我会从那里开始。
一旦你找到了问题的参数,它可能是显而易见的,有VB.NET和Access或你们之间的数据类型冲突或不匹配,可能需要做一些试验有点找哪个VB投会工作。在你的例子中,下一个最有可能的候选人将是一个字符串长度问题,并且通过查看表定义和文本框输入长度限制,这将非常明显。
我的猜测是,这条线抛出异常:
cmd.Parameters.Add(New OleDbParameter("Age", CType(TextBox2.Text, Integer)))
我的第一个建议是使用适当的控制来处理数字输入,如NumericUpDown控件。
我的第二个建议(如果继续使用TextBox)是显式转换使用Integer.TryParse的字符串值的整数。
我的第三个建议是停止使用字符串值CTYPE(textBox中[N]。文本),以将它们转换为字符串,它是不必要的。
我的第四个建议是使用Parameter.AddWithValue,而不是因为它会用传递的值的数据类型。
我的第五次和最后的建议是包装你的对象,在使用声明或实现IDisposable至少明确处置。
这里是实施建议2-5个例子,如果你把我的第一个建议,然后#2是不必要的:
'Declare an age variable that is an Integer
Dim age As Integer
'Convert the String to an Integer
If Integer.TryParse(TextBox2.Text, age) Then
'Declare the connection object
Dim con As OleDbConnection
'Database operations should always be wrapped in Try/Catch
Try
'Set the connection object to a new instance
con = New OleDbConnection(connString)
'Create a new instance of the command object
Using cmd As OleDbCommand = New OleDbCommand("INSERT INTO [Dnr_tbl] ([Dname], [Age], [Bloodgroup], [Location], [Contact], [Email]) VALUES (@name, @age, @bloodgroup, @location, @contact, @email)", con)
'Parameterize the query
With cmd.Parameters
.AddWithValue("@name", TextBox1.Text)
.AddWithValue("@age", age)
.AddWithValue("@bloodgroup", TextBox3.Text)
.AddWithValue("@location", TextBox4.Text)
.AddWithValue("@contact", TextBox5.Text)
.AddWithValue("@email", TextBox6.Text)
End With
'Open the connection
con.Open()
'Execute the query
cmd.ExecuteNonQuery()
'Close the connection
con.Close()
'Clear the controls
TextBox1.Clear() : TextBox2.Clear() : TextBox3.Clear() : TextBox4.Clear() : TextBox5.Clear() : TextBox6.Clear()
End Using
Catch ex As Exception
MessageBox.Show(ex.ToString())
Finally
'Check if the connection object was initialized
If con IsNot Nothing Then
If con.State = ConnectionState.Open Then
'Close the connection if it was left open(exception thrown)
con.Close()
End If
'Dispose of the connection object
con.Dispose()
End If
End Try
Else
MessageBox.Show("Invalid age input.")
End If
从这里:https://social.msdn.microsoft.com/Forums/vstudio/EN-US/853aec35-5b19-4126-b142-44dae2ad0a47/oledbexception溢出?论坛= vbgeneral我相信你的插入查询可能是不正确的。 – Jaxi
如果不查看表结构,很难判断查询参数是否有误。最可疑的部分是'cmd.Parameters.Add(New OleDbParameter(“Age”,CType(TextBox2.Text,Integer)))',因为'Age'列经常使用'Byte'数据类型。 –
就像一个笔记 - 存储捐助者的年龄不是一个好主意 - 我会存储他们的DOB。确保你的字符串是正确的长度。此外,VB.NET中的整数是4个字节,而它们在Access中是2个字节。你可能想在.NET中使用'Int16'。 – Paul