简体   繁体   English

解决方案将 SQL 中的文本框添加为 vb.net 中的字符串

[英]Solution add textbox in SQL as string in vb.net

What is the solution if you see the code below there is a value (2000) then I want to change it to textbox so I can custom value.如果您看到下面的代码有一个值(2000),那么解决方案是什么,那么我想将其更改为文本框,以便我可以自定义值。

Thanks谢谢

jack杰克

Dim sql As String = "update GSDTS as t1 inner join IFGTS as t2 on t1.[ITM] = t2.[ITM] set t1.[CIU] = t2.[PRSOBNET]+2000 WHERE GDN = 'A.04.01.002.001'AND PNM=@PNM"
Using conn As New OleDbConnection(cn),
                    cmd As New OleDbCommand(sql, conn)
    cmd.Parameters.AddWithValue("@PNM", ComboBox1.SelectedValue)
    conn.Open()
    cmd.ExecuteNonQuery()
End Using

You can just add another parameter to your query like here您可以像这里一样向您的查询添加另一个参数

' Read the textbox value here'
Dim newValue as Integer
Int32.TryParse(txtInput.Text, newValue)

' Put the parameter placeholder instead of a constant'
Dim sql As String = "update GSDTS as t1 inner join IFGTS as t2 
                                  on t1.[ITM] = t2.[ITM] 
                     set t1.[CIU] = t2.[PRSOBNET]+@addvalue
                     WHERE GDN = 'A.04.01.002.001'AND PNM=@PNM"
Using conn As New OleDbConnection(cn),
     cmd As New OleDbCommand(sql, conn)
     ' Add the other parameter and its value before the old one. See note below'
     cmd.Parameters.AddWithValue("@addvalue", newValue)

     cmd.Parameters.AddWithValue("@PNM", ComboBox1.SelectedValue)

     conn.Open()
     cmd.ExecuteNonQuery()
End Using

Also, take a look at how to replace AddWithValue with a single line Add.另外,看看如何用单行 Add 替换 AddWithValue。 AddWithValue is dangerous because it can misrepresents your value when working with dates or decimals and it is inefficient with strings AddWithValue 很危险,因为它在处理日期或小数时可能会歪曲您的值,并且使用字符串效率低下

** IMPORTANT ** ** 重要的 **
You are working with the OleDb provider.您正在使用 OleDb 提供程序。 In this context the parameters should be added in the same order in which their placeholders appear in the string.在这种情况下,参数的添加顺序应与它们的占位符在字符串中出现的顺序相同。 So the new parameter should be added before the @PNM one otherwise they will be read in the wrong order when the query is executed.所以应该在@PNM 之前添加新参数,否则在执行查询时会以错误的顺序读取它们。

Your SQL syntax wouldn't work for any RDBMS I know of and you didn't tag the backend.您的 SQL 语法不适用于我知道的任何 RDBMS,并且您没有标记后端。 That might be solely a reason for it not to work at all, you should start by correcting it.这可能仅仅是它根本不起作用的一个原因,你应该从纠正它开始。 Second, do not use AddWithValue but Add and explicitly specify your data type.其次,不要使用 AddWithValue 而是 Add 并明确指定您的数据类型。 Having said that, for example MS SQL server as a backend:话虽如此,例如将 MS SQL 服务器作为后端:

    Dim sql As String = <sql>UPDATE GSDTS
SET [CIU] = t2.[PRSOBNET] + @addVal
FROM GSDTS AS t1
    INNER JOIN IFGTS AS t2
        ON t1.[ITM] = t2.[ITM]
WHERE t1.GDN = 'A.04.01.002.001'
      AND t1.PNM = @PNM;</sql>

    Dim addVal As Integer = Nothing
    If Integer.TryParse(txtAdd.Text, addVal) Then
    Using conn As New OleDbConnection(cn),
                  cmd As New OleDbCommand(sql, conn)
            cmd.Parameters.Add("@addVal", OleDbType.Integer).Value = addVal
            cmd.Parameters.Add("@PNM", OleDbType.VarChar).Value = ComboBox1.SelectedValue
            conn.Open()
        cmd.ExecuteNonQuery()
        End Using
    End If

Note that order of variable declarations are same as their order they are used in the query.请注意,变量声明的顺序与它们在查询中使用的顺序相同。 That is a necessity with OleDb (positional arguments).这是 OleDb(位置参数)的必要条件。 If your backend is something like MS SQL server then prefer using backend specific SqlConnection, SqlCommand (then you can also use named parameters).如果您的后端类似于 MS SQL 服务器,那么更喜欢使用后端特定的 SqlConnection、SqlCommand(那么您也可以使用命名参数)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM