我正在嘗試更新訪問資料庫表中的現有記錄并保存一個 pdf 檔案。但是,我收到“沒有為一個或多個引數提供值”錯誤。
請看下面的代碼。任何幫助都感激不盡。
謝謝
Dim strsql As String
Dim settings As String = ConfigurationManager.ConnectionStrings("ABCBO.My.MySettings.db_abcdealsConnectionString").ConnectionString
Dim con As New OleDbConnection
Private Sub printtopdf_Click(sender As Object, e As EventArgs) Handles printtopdf.Click
con.ConnectionString = settings
Try
Dim fs As New FileStream("E:\NewApp\test1.pdf", FileMode.Open, FileAccess.Read)
strsql = "UPDATE ABC_DEALS SET CONFIRMATION_COPY = @AT1 WHERE DEAL_TICKET = '" & TextBox5.Text & "'"
Dim cmd As New OleDbCommand(strsql, con)
Dim byteArr(CInt(fs.Length)) As Byte
fs.Read(byteArr, 0, fs.Length)
fs.Close()
con.Open()
cmd.Parameters.Add("@AT1", OleDbType.Binary).Value = byteArr
cmd.ExecuteNonQuery()
con.Close()
Catch exc As Exception
MsgBox(exc.Message)
con.Close()
End Try
End Sub
uj5u.com熱心網友回復:
FileStream、OleDbConnection和OleDbCommand都有一個Dispose需要呼叫的方法,以便可以釋放非托管資源。Vb.net 提供了Using...End Using塊來為我們處理這個問題,并關閉連接和流。Using資料訪問代碼中的塊包括連接和命令。注意連接線后面的逗號。
您試圖在 click 方法中做太多事情。將資料訪問代碼與其他代碼分開。
始終使用引數。用戶輸入之類的TextBox5.Text可能會將惡意代碼引入您的資料庫。引數值不被資料庫視為可執行代碼。
將連接字串傳遞給連接的建構式。
將CommandText和傳遞Connection給命令的建構式。
直到直接在之前不要打開連接 Execute...
即使這不能解決問題,它也會讓您更清楚地了解問題出在哪里。放置一個斷點并逐步執行代碼檢查變數值。
Private settings As String = ConfigurationManager.ConnectionStrings("ABCBO.My.MySettings.db_abcdealsConnectionString").ConnectionString
Private Sub printtopdf_Click(sender As Object, e As EventArgs) Handles Button1.Click 'printtopdf.Click
Dim byteArr As Byte()
Dim path As String = "E:\NewApp\test1.pdf"
Try
Using fs As New FileStream(path, FileMode.Open, FileAccess.Read)
ReDim byteArr(CInt(fs.Length))
fs.Read(byteArr, 0, CInt(fs.Length))
End Using
Dim Updated = UpdateDatabase(byteArr, CInt(TextBox5.Text))
MessageBox.Show($"{Updated} has been successfully updated.")
Catch exc As Exception
MsgBox(exc.Message)
End Try
End Sub
Private Function UpdateDatabase(PDF As Byte(), Ticket As Integer) As Integer
Dim strsql = "UPDATE ABC_DEALS SET CONFIRMATION_COPY = @AT1 WHERE DEAL_TICKET = @Ticket;"
Dim RecordsUpdated As Integer
Using con As New OleDbConnection(settings),
cmd As New OleDbCommand(strsql, con)
cmd.Parameters.Add("@AT1", OleDbType.Binary).Value = PDF
cmd.Parameters.Add("@Ticket", OleDbType.Integer).Value = Ticket
con.Open()
RecordsUpdated = cmd.ExecuteNonQuery()
End Using
Return RecordsUpdated
End Function
編輯
Private Function GetByteArrayFromFile(FilePath As String) As Byte()
Dim file As Byte()
Using stream As New FileStream(FilePath, FileMode.Open, FileAccess.Read),
reader As New BinaryReader(stream)
file = reader.ReadBytes(CInt(stream.Length))
End Using
Return file
End Function
Private Sub printtopdf_Click(sender As Object, e As EventArgs) Handles Button1.Click 'printtopdf.Click
Dim path As String = "E:\NewApp\test1.pdf"
Dim byteArr = GetByteArrayFromFile(path)
Try
Dim Updated = UpdateDatabase(byteArr, CInt(TextBox5.Text))
MessageBox.Show($"{Updated} has been successfully updated.")
Catch exc As Exception
MsgBox(exc.Message)
End Try
End Sub
編輯 2
我看到 3 個地方需要更改代碼以容納 Long。我TryParse在代碼中添加了驗證以確保輸入是正確的型別。
舊代碼 1
Dim Updated = UpdateDatabase(byteArr, CInt(TextBox5.Text))
新代碼 1
Dim ticket As Long
If Not Long.TryParse(TextBox5.Text, ticket) Then
MessageBox.Show("Please enter a valid ticket number")
Exit Sub
End If
Dim Updated = UpdateDatabase(byteArr, ticket)
舊代碼 2
Private Function UpdateDatabase(PDF As Byte(), Ticket As Integer) As Integer
新代碼 2 注意:回傳型別保持 Integer。
Private Function UpdateDatabase(PDF As Byte(), Ticket As Long) As Integer
舊代碼 3
cmd.Parameters.Add("@Ticket", OleDbType.Integer).Value = Ticket
新代碼 3 似乎 OleDbType.BigInt 映射到 Int64。在 Access 中,該欄位應為 Number 8 位元組。對此并不積極。你可能需要玩一下。
cmd.Parameters.Add("@Ticket", OleDbType.BigInt).Value = Ticket
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/314925.html
標籤:网络
下一篇:類中通用長度的二維陣列
