2017-10-18 148 views
0

連接到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 

And here is the snapshot of my error message

+0

從這裏:https://social.msdn.microsoft.com/Forums/vstudio/EN-US/853aec35-5b19-4126-b142-44dae2ad0a47/oledbexception溢出?論壇= vbgeneral我相信你的插入查詢可能是不正確的。 – Jaxi

+0

如果不查看錶結構,很難判斷查詢參數是否有誤。最可疑的部分是'cmd.Parameters.Add(New OleDbParameter(「Age」,CType(TextBox2.Text,Integer)))',因爲'Age'列經常使用'Byte'數據類型。 –

+0

就像一個筆記 - 存儲捐助者的年齡不是一個好主意 - 我會存儲他們的DOB。確保你的字符串是正確的長度。此外,VB.NET中的整數是4個字節,而它們在Access中是2個字節。你可能想在.NET中使用'Int16'。 – Paul

回答

-1

首先嚐試縮小其參數引起的問題。您可以註釋掉所有參數並將其添加回來,直到找到問題(根據需要調整SQL語句以匹配參數計數),或者選擇可能的嫌疑犯並將其註釋掉,並繼續評論參數並調整SQL語句直到它工作。您可能需要暫時調整Access中的表格以允許測試列中的空值。整數值似乎是一個可能的候選人,我會從那裏開始。

一旦你找到了問題的參數,它可能是顯而易見的,有VB.NET和Access或你們之間的數據類型衝突或不匹配,可能需要做一些試驗有點找哪個VB投會工作。在你的例子中,下一個最有可能的候選人將是一個字符串長度問題,並且通過查看錶定義和文本框輸入長度限制,這將非常明顯。

0

我的猜測是,這條線拋出異常:

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