【问题标题】:How set a variable in Mysql from Visual Basic .net using parametrized Value from a Texbox如何使用文本框中的参数化值从 Visual Basic .net 在 Mysql 中设置变量
【发布时间】:2014-08-27 03:14:25
【问题描述】:

我有这个存储过程并且工作得很好,但我需要从 Visual Basic .net 传递变量“@item”,代码使用直接查询工作,但它很混乱和不安全我需要参数化,从文本框发送值。

SET @item = '@vbitem';
SELECT
rm_users.username,
rm_users.enableuser,
rm_users.firstname,
rm_users.lastname,
rm_users.company,
rm_users.phone,
rm_users.mobile,
rm_users.address,
rm_users.city,
rm_users.srvid,
rm_users.staticipcpe,
rm_users.contractid,
rm_users.email,
radacct.nasipaddress,
radacct.callingstationid,
radacct.framedipaddress,
rm_wlan.maccpe,
rm_wlan.ccq,
rm_wlan.`signal`,
rm_wlan.snr,
rm_wlan.apip,
rm_wlan.`timestamp`,
nas.nasname,
nas.shortname,
radacct.acctstoptime
FROM
rm_users
INNER JOIN radacct ON rm_users.username = radacct.username
INNER JOIN rm_wlan ON radacct.callingstationid = rm_wlan.maccpe
INNER JOIN nas ON radacct.nasipaddress = nas.nasname
WHERE
rm_users.username LIKE @item OR
rm_users.firstname LIKE @item OR
rm_users.lastname LIKE @item OR
rm_users.city LIKE @item OR
rm_users.address LIKE @item OR
rm_users.taxid LIKE @item OR
rm_users.company LIKE @item OR
radacct.callingstationid LIKE @item
GROUP BY
rm_users.username
ORDER BY
rm_users.username ASC here

Visual Basic 代码提取

         cmd = New MySqlCommand("procedure-name")
         cmd.Parameters.AddWithValue("@vbitem", textbox.Text)
         cmd.CommandType = CommandType.StoredProcedure

【问题讨论】:

    标签: mysql vb.net stored-procedures sql-parametrized-query


    【解决方案1】:

    如果你的 sproc 有一个 @item 参数,那么你应该去掉 sproc 中的第一行,然后在 VB 代码中设置 @item 参数,即

    cmd.Parameters.AddWithValue("@item", textbox.Text)
    

    【讨论】:

    • 但我必须在 proc 中设置 SET @item = '@item';还是像我发帖一样留下它?
    • 嗯,也不是。你照我说的做:完全摆脱第一行。如果您在存储过程中声明了一个名为 @item 的参数,那么我提供的 VB 代码行就是设置该参数值的内容。您无需在 SQL 代码中手动执行此操作。
    • 我是这样做的 code cmd = New MySqlCommand("show_all") cmd.Parameters.AddWithValue("@item", tbsearch.Text) cmd.CommandType = CommandType.StoredProcedure 我的问题我必须从 proc 中删除它吗codeBEGIN SET @item = @item;选择
    • 鉴于我已经告诉过你两次要删除该行,你为什么还要问这个问题?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多