将多个值添加到 SQL 服务器 VB.NET 数据库
Add multiple values to a SQL Server VB.NET Database
我正在尝试使用 VB.NET 向 SQL 服务器数据库添加多个值。
我已经包含了以下命名空间:
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.SqlServerCe
启动时,我已声明 SQL 连接:
con.ConnectionString = "Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=""G:\Program\X\Database1.mdf"";Integrated Security=True"
我创建了一个 VB Windows 表单,使用户能够添加、编辑和删除试卷的问题。
当用户单击 "Save Questions" 按钮时,问题将保存为 .txt 文件。
这应该会调用 InsertQuestion
子例程:
con.Open()
InsertQuestion(con)
con.Close()
InsertQuestion
子程序:
Sub InsertQuestion(ByVal con As SqlConnection)
Using con
Dim command As New SqlCommand(("INSERT INTO Table VALUES('" & Collection(0).Question & "','" & Collection(0).Answer & "','" & Collection(0).Type & "','" & Collection(0).Mark & "')'"), con)
command.ExecuteNonQuery()
con.Close()
End Using
End Sub
这应该将此数据添加到 table。 table 有五列 - ID、问题、答案、类型、标记。 ID为题号,设置为自增。
从数组的第一个元素开始,索引 (0) 中的问题应添加到第 2 列(问题下方),索引 (0) 中的答案应添加到第 3 列(答案下方)...以及等等。
但是,当程序为运行,用户点击"Save Questions",出现错误:
An unhandled exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll
Additional information: Operator '&' is not defined for string "INSERT INTO QuestionTable VALUES" and type 'RuntimeType'.
因此,我将非常感谢有关如何修复此 command/code 以使数据能够添加到 table 的建议。
此外,我将如何从索引 1 等向 table 添加更多问题...
非常感谢。
如评论中所述,创建参数化命令可以避免SQL语句中的错误。
另外,回答你的另一个问题,创建事务是一种一次向数据库插入多个值的方法:
Sub InsertQuestion(ByVal con As SqlConnection)
Using con
Dim command As New SqlCommand("INSERT INTO Table VALUES(@Question, @Answer, @Type, @Mark)", con)
command.Parameters.Add("@Question", YourType)
command.Parameters.Add("@Answer", YourType)
command.Parameters.Add("@Type", YourType)
command.Parameters.Add("@Mark", YourType)
command.Transaction = command.Connection.BeginTransaction
For i = 0 To Collection.Count - 1
command.Parameters("@Question").Value = Collection(i).Question
command.Parameters("@Answer").Value = Collection(i).Answer
command.Parameters("@Type").Value = Collection(i).Type
command.Parameters("@Mark").Value = Collection(i).Mark
command.ExecuteNonQuery()
Next
command.Transaction.Commit()
con.Close()
End Using
End Sub
我正在尝试使用 VB.NET 向 SQL 服务器数据库添加多个值。
我已经包含了以下命名空间:
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.SqlServerCe
启动时,我已声明 SQL 连接:
con.ConnectionString = "Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=""G:\Program\X\Database1.mdf"";Integrated Security=True"
我创建了一个 VB Windows 表单,使用户能够添加、编辑和删除试卷的问题。
当用户单击 "Save Questions" 按钮时,问题将保存为 .txt 文件。
这应该会调用 InsertQuestion
子例程:
con.Open()
InsertQuestion(con)
con.Close()
InsertQuestion
子程序:
Sub InsertQuestion(ByVal con As SqlConnection)
Using con
Dim command As New SqlCommand(("INSERT INTO Table VALUES('" & Collection(0).Question & "','" & Collection(0).Answer & "','" & Collection(0).Type & "','" & Collection(0).Mark & "')'"), con)
command.ExecuteNonQuery()
con.Close()
End Using
End Sub
这应该将此数据添加到 table。 table 有五列 - ID、问题、答案、类型、标记。 ID为题号,设置为自增。
从数组的第一个元素开始,索引 (0) 中的问题应添加到第 2 列(问题下方),索引 (0) 中的答案应添加到第 3 列(答案下方)...以及等等。
但是,当程序为运行,用户点击"Save Questions",出现错误:
An unhandled exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll
Additional information: Operator '&' is not defined for string "INSERT INTO QuestionTable VALUES" and type 'RuntimeType'.
因此,我将非常感谢有关如何修复此 command/code 以使数据能够添加到 table 的建议。
此外,我将如何从索引 1 等向 table 添加更多问题...
非常感谢。
如评论中所述,创建参数化命令可以避免SQL语句中的错误。
另外,回答你的另一个问题,创建事务是一种一次向数据库插入多个值的方法:
Sub InsertQuestion(ByVal con As SqlConnection)
Using con
Dim command As New SqlCommand("INSERT INTO Table VALUES(@Question, @Answer, @Type, @Mark)", con)
command.Parameters.Add("@Question", YourType)
command.Parameters.Add("@Answer", YourType)
command.Parameters.Add("@Type", YourType)
command.Parameters.Add("@Mark", YourType)
command.Transaction = command.Connection.BeginTransaction
For i = 0 To Collection.Count - 1
command.Parameters("@Question").Value = Collection(i).Question
command.Parameters("@Answer").Value = Collection(i).Answer
command.Parameters("@Type").Value = Collection(i).Type
command.Parameters("@Mark").Value = Collection(i).Mark
command.ExecuteNonQuery()
Next
command.Transaction.Commit()
con.Close()
End Using
End Sub