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

发布于 2025-01-28 09:31:12 字数 479 浏览 3 评论 0原文

解决方案是什么,如果您看到下面的代码有一个值(2000),那么我想将其更改为文本框,以便我可以自定义值。

谢谢杰克

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

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心安伴我暖 2025-02-04 09:31:12

您可以像在此处一样将另一个参数添加到查询中

' 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

,看看如何用单行添加替换addWithValue。 AddWithValue很危险,因为它可以在使用日期或小数时歪曲您的价值,并且效率降低了字符串

**重要**
您正在与OLEDB提供商合作。在这种情况下,应按照占位符出现在字符串中的占位符的顺序添加参数。因此,应在@pnm之前添加新参数,否则在执行查询时将以错误的顺序读取它们。

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. AddWithValue is dangerous because it can misrepresents your value when working with dates or decimals and it is inefficient with strings

** IMPORTANT **
You are working with the OleDb provider. 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.

掩于岁月 2025-02-04 09:31:12

您的SQL语法对我所知道的任何RDBM都无法使用,也没有标记后端。这可能完全是因为它根本不工作的原因,您应该首先纠正它。
其次,请勿使用addWithValue,而是添加并明确指定您的数据类型。
话虽如此,例如,MS SQL Server作为后端:

    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

请注意,可变声明的顺序与它们在查询中使用的顺序相同。这是OLEDB(位置论点)的必要条件。如果您的后端类似于MS SQL Server,则更喜欢使用特定的SQLConnection,SQLCommand(那么您也可以使用命名参数)。

Your SQL syntax wouldn't work for any RDBMS I know of and you didn't tag the backend. 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.
Having said that, for example MS SQL server as a backend:

    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). If your backend is something like MS SQL server then prefer using backend specific SqlConnection, SqlCommand (then you can also use named parameters).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文