![](/img/trans.png)
[英]How can i add values to a user's properties and then save it to a database?
[英]How can I save this in a database?
嘗試將數據插入sql數據庫時遇到問題。 我希望有一個人可以幫助我。 每次我單擊button2時,都會出現一個MessageBox並說:
在表中插入記錄時出錯...“行”不是公認的內置函數名稱。
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim con As New SqlConnection
Dim cmd As New SqlCommand
Using sr As New System.IO.StreamReader("C:\Users\klaasjelle\Documents\Visual Studio 2017\Projects\WindowsApp2\WindowsApp2\bin\Debug\Images.txt")
Try
con.ConnectionString = "Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\klaasjelle\Documents\visual studio 2017\Projects\WindowsApp3\WindowsApp3\Niks.mdf;Integrated Security=True"
con.Open()
cmd.Connection = con
cmd.CommandText = "INSERT INTO Data (FilePath, ImageSize, ImageSide) VALUES (ListViewItem(lines(0)), lvItem.SubItem.Add(lines(1)), lvItem.SubItem.Add(lines(2)))"
cmd.ExecuteNonQuery()
Catch ex As Exception
MessageBox.Show("Error while inserting record on table..." & ex.Message, "Insert Records")
Finally
con.Close()
End Try
End Using
End Sub
End Class
就像我說的。 每次我單擊Button2時,都會說:
在表上插入記錄時出錯...“行”不是公認的內置函數名稱。
有人能幫助我嗎
如此處所建議的,也是屬於Button1_Click的代碼
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Using sr As New System.IO.StreamReader("C:\Users\klaasjelle\Documents\Visual Studio 2017\Projects\WindowsApp2\WindowsApp2\bin\Debug\Images.txt")
While Not sr.EndOfStream
Dim lines As String() = sr.ReadLine.Split(New String() {","}, StringSplitOptions.RemoveEmptyEntries)
Dim lvItem As New ListViewItem(lines(0))
lvItem.SubItems.Add(lines(1))
lvItem.SubItems.Add(lines(2))
ListView1.Items.Add(lvItem)
End While
End Using
ListView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent)
ListView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize)
End Sub
您正在將字符串文字傳遞給INSERT語句。 該字符串不是ListView項,而僅僅是數據庫引擎嘗試解析的字符串,它完全了解列表視圖和lines數組。
相反,您應該使用參數化查詢
cmd.CommandText = "INSERT INTO Data (FilePath, ImageSize, ImageSide)
VALUES (@path, @size, @side)"
cmd.Parameters.Add("@path", SqlDbType.NVarChar).Value = ListViewItem(lines(0))
cmd.Parameters.Add("@size", SqlDbType.NVarChar).Value = ListViewItem(lines(1))
cmd.Parameters.Add("@side", SqlDbType.NVarChar).Value = ListViewItem(lines(2))
....
我假設您的數據庫字段的類型為NVarChar,如果不是,則將SqlDbType枚舉更改為正確的值,並將輸入的ListViewItem解析為正確的數據類型。
我的回答僅限於數據庫問題,但是您的代碼似乎不完整。 在哪里定義lines數組? 您如何獲得對ListViewItem的引用?
編輯之后:
變量是在button1_click事件中定義的,因此您不能在button2_click事件中使用它們。 您需要閱讀列表視圖的內容並提取在button1_click中添加的信息。
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button1.Click
using con As New SqlConnection
using cmd As New SqlCommand
con.ConnectionString = "....."
con.Open()
cmd.Connection = con
cmd.Parameters.Add("@path", SqlDbType.NVarChar)
cmd.Parameters.Add("@size", SqlDbType.NVarChar)
cmd.Parameters.Add("@side", SqlDbType.NVarChar)
cmd.CommandText = "INSERT INTO Data (FilePath, ImageSize, ImageSide)
VALUES (@path, @size, @side)"
For Each item as ListViewItem in ListView1
Dim path = item.Text
Dim size = item.SubItems(1)
Dim side = item.SubItems(2)
' Now you have the variables to insert in the database
cmd.Parameters("@path").Value = path
cmd.Parameters("@size").Value = size
cmd.Parameters("@side").Value = side
cmd.ExecuteNonQuery()
Next
End Using
End Using
End Sub
注意,我改變了上一個示例的邏輯。 現在,我在Using塊中創建連接和命令。 這將使我免於顯式關閉和銷毀這些命令。 然后,在循環外定義參數,而在循環內僅更改值。 (除非您的列表視圖中有成千上萬個項目,否則沒有大的好處的簡單優化)最后,我遍歷列表視圖中的每個項目,並從每個ListViewItem中提取要插入數據庫的信息
您正在嘗試在SQL中執行VB代碼。 當然哪個不起作用。 在VB代碼中獲取值,然后將它們作為參數添加到SQL命令中。 像這樣:
cmd.CommandText = "INSERT INTO Data (FilePath, ImageSize, ImageSide) VALUES (@FilePath, @ImageSize, @ImageSide)"
command.Parameters.Add("@FilePath", SqlDbType.NVarChar) 'Guessing on the type here
command.Parameters("@FilePath").Value = ListViewItem(lines(0))
' Repeat for the other two parameters, using the appropriate SqlDbType values for each
cmd.ExecuteNonQuery()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.