在 vb.net 中将 'String' 转换为 'SQlCommand'
Convert 'String' to 'SQlCommand' in vb.net
我是一个新手vb程序员。我确信这很简单,但我做对了。
Value of type 'String' cannot be converted to 'SQLCommand'
是错误。
Public Sub InsertPackageDetails(ByVal PackageName As String, ByVal PackageDescription As String, ByVal DateOfInstallation As DateTime, ByVal ImportPackageStatus As String)
Dim SQL As String = "Insert into PackageInstallation(PackageName,PackageDescription,DateOfInstallation,ImportPackageStatus) values('" + PackageName + "','" + PackageDescription + "','" + DateOfInstallation + "'," + ImportPackageStatus + ") "
DBHelper2.ExecuteNonQuery(SQL)
End Sub
如何将字符串 SQL 转换为 SQL 命令?
感谢您的帮助。
您可以在此处查看创建命令的各种方法。 https://docs.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlcommand.-ctor?view=dotnet-plat-ext-3.1
您应该使用 Using...End Using 块,以便您的连接和命令将被关闭和处理,但您需要在使用它们的本地方法中拥有这些对象。
我不得不猜测数据库中您的字段的类型和大小。检查数据库并相应地更正代码。
Public Sub InsertPackageDetails(ByVal PackageName As String, ByVal PackageDescription As String, ByVal DateOfInstallation As DateTime, ByVal ImportPackageStatus As String)
Dim SQL As String = "Insert into PackageInstallation(PackageName,PackageDescription,DateOfInstallation,ImportPackageStatus)
Values(@PackageName, @PackageDescription, @DateOfInstallation, @ImportPackageStatus;"
Using cn As New SqlConnection("Your connection string"),
cmd As New SqlCommand(SQL, cn)
With cmd.Parameters
.Add("@PackageName", SqlDbType.VarChar, 100).Value = PackageName
.Add("@PackageDescription", SqlDbType.VarChar, 100).Value = PackageDescription
.Add("@DateOfInstallation", SqlDbType.DateTime).Value = DateOfInstallation
.Add("@ImportPackageStatus", SqlDbType.VarChar, 100).Value = ImportPackageStatus
End With
cn.Open()
cmd.ExecuteNonQuery()
End Using
End Sub
我是一个新手vb程序员。我确信这很简单,但我做对了。
Value of type 'String' cannot be converted to 'SQLCommand'
是错误。
Public Sub InsertPackageDetails(ByVal PackageName As String, ByVal PackageDescription As String, ByVal DateOfInstallation As DateTime, ByVal ImportPackageStatus As String)
Dim SQL As String = "Insert into PackageInstallation(PackageName,PackageDescription,DateOfInstallation,ImportPackageStatus) values('" + PackageName + "','" + PackageDescription + "','" + DateOfInstallation + "'," + ImportPackageStatus + ") "
DBHelper2.ExecuteNonQuery(SQL)
End Sub
如何将字符串 SQL 转换为 SQL 命令? 感谢您的帮助。
您可以在此处查看创建命令的各种方法。 https://docs.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlcommand.-ctor?view=dotnet-plat-ext-3.1
您应该使用 Using...End Using 块,以便您的连接和命令将被关闭和处理,但您需要在使用它们的本地方法中拥有这些对象。
我不得不猜测数据库中您的字段的类型和大小。检查数据库并相应地更正代码。
Public Sub InsertPackageDetails(ByVal PackageName As String, ByVal PackageDescription As String, ByVal DateOfInstallation As DateTime, ByVal ImportPackageStatus As String)
Dim SQL As String = "Insert into PackageInstallation(PackageName,PackageDescription,DateOfInstallation,ImportPackageStatus)
Values(@PackageName, @PackageDescription, @DateOfInstallation, @ImportPackageStatus;"
Using cn As New SqlConnection("Your connection string"),
cmd As New SqlCommand(SQL, cn)
With cmd.Parameters
.Add("@PackageName", SqlDbType.VarChar, 100).Value = PackageName
.Add("@PackageDescription", SqlDbType.VarChar, 100).Value = PackageDescription
.Add("@DateOfInstallation", SqlDbType.DateTime).Value = DateOfInstallation
.Add("@ImportPackageStatus", SqlDbType.VarChar, 100).Value = ImportPackageStatus
End With
cn.Open()
cmd.ExecuteNonQuery()
End Using
End Sub