【问题标题】:"Conversion from string "insert into agent values(" to type 'Double' is not valid." in vb.net“从字符串“插入代理值(”到类型“双”的转换无效。”在 vb.net
【发布时间】:2018-03-27 02:41:33
【问题描述】:
Private Sub recruit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles recruit.Click
    Dim query3 As String
    Dim n As Integer
    Dim query2 As String = "select max(stag) from agent"
    con.ConnectionString = ("Data Source=DESKTOP-CTN5IJ3\SQLEXPRESS;Integrated Security=True")
    Dim autono As New SqlCommand(query2, con)
    con.Open()
    If IsDBNull(autono.ExecuteScalar) Then
        n = 7
    Else
        n = autono.ExecuteScalar + 5
    End If
    con.Close()
    query3 = "insert into agent values(" + n + ",'" + ncrypt(txtssn.Text) + "','" + ncrypt(txtname.Text) + "','" + ncrypt(txtadd.Text) + "',0,0,'newbpwd')"
    Dim save As New SqlCommand(query3, con)
    con.Open()
    save.ExecuteNonQuery()
    con.Close()
End Sub

//query3 = "插入代理值(" + n + ",'" + ncrypt(txtssn.Text) + "','" + ncrypt(txtname.Text) + "','" + ncrypt( txtadd.Text) + "',0,0,'newbpwd')" 这就是问题所在

【问题讨论】:

标签: sql vb.net


【解决方案1】:

您的主要问题是您使用了错误的字符串连接运算符。使用& 而不是+

另一种方法是使用字符串插值

Dim n As Integer = 101
Dim query = $"INSERT INTO table VALUES ({n})"

但是,如果您从一开始就正确使用 SqlParameters 将值传递给 sql 查询 - 您根本不需要连接字符串。
通过使用SqlParameter,您将保护您的当前代码免受可能的 Sql 注入。

Private Sub recruit_Click(sender As Object, e As EventArgs) Handles recruit.Click
    Dim connectionString = _
        "Data Source=DESKTOP-CTN5IJ3\SQLEXPRESS;Integrated Security=True"
    Dim newId As Integer = 7

    Using connection As New SqlConnection(connectionString)
        Dim query As String = "select max(stag) from agent"
        Using command As New SqlCommand(query, connection)
            connection.Open()
            Dim result = command.ExecuteScalar()
            If result IsNot DbNull.Value Then
                newId = DirectCast(result, Integer) + 5
            End If
        End Using
    End Using


    Using connection As New SqlConnection(connectionString)
        Dim query As String = _
            "insert into agent values (@Id, @SSN, @Name, @Add, 0, 0, 'newbpwd')"
        Using command As New SqlCommand(query, connection)
            Dim parameters As New List(Of SqlParameter) From
            {
                New SqlParameter With { .ParameterName = "@Id", .SqlDbType = SqlDbType.Int, .Value = newId },
                New SqlParameter With { .ParameterName = "@SSN", .SqlDbType = SqlDbType.VarChar, .Value = ncrypt(txtssn.Text)},
                New SqlParameter With { .ParameterName = "@Name", .SqlDbType = SqlDbType.VarChar, .Value = ncrypt(txtname.Text)},
                New SqlParameter With { .ParameterName = "@Add", .SqlDbType = SqlDbType.VarChar, .Value = ncrypt(txtadd.Text)},
            }
            command.Parameters.AddRange(parameters)

            connection.Open()
            command.ExecuteNonQuery()
        End Using
    End Using
End Sub

【讨论】:

  • 我需要使用任何头文件吗?
  • “头”文件是什么意思?
  • System.Data.SqlClient 是我在程序中导入的头文件
  • 我认为 Visual Studio 会建议应该将哪些“使用”指令添加到文件中。 System.Data.SqlClient 其中一个
  • 在代码的两行发现错误 1. 如果结果 DbNull.Value 那么这里“操作符” 没有为对象和 system.dbnull.values 定义 2.command.Parameters。 AddRange(parameters) here "'parameters' is not declared"
【解决方案2】:

在 vb 中,你使用 '&' 符号而不是 '+' 来连接字符串

【讨论】:

    【解决方案3】:

    我的方法是在将整数插入表之前将其转换为字符串,它适用于我。例如:

        Dim n As Integer = 33
        Dim s As String = n.ToString
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-28
      • 1970-01-01
      • 1970-01-01
      • 2016-08-09
      • 2013-03-08
      • 2011-07-30
      相关资源
      最近更新 更多