0

I cannot seem to figure out how to get an output of a SQL query as a variable in VB. I need something like what's below so that I can use that variable in another SQL query. SQL doesn't allow the use of variables as column headers when running queries, so I thought I could use VB to insert the actual output of one SQL task as the dynamic variable in a loop of update queries. Let me (hopefully) explain.

I have the following query:

DECLARE @id int = (SELECT max(id) FROM [views]), @ViewType nvarchar(3);

WHILE @id IS NOT NULL
BEGIN

    SELECT @ViewType = (SELECT [View] FROM [views] WHERE id = @id);

    UPDATE a
    SET a.[@ViewType] = '1'
    FROM [summary] a
    INNER JOIN [TeamImage] b
    ON a.[Part_Number] = b.[PartNum]

    WHERE b.[View] = @ViewType;

SELECT @id = max(id) FROM [views] WHERE id < @id;

END;

The SET a.[@ViewType] = '1' obviously will not work in SQL. But if I could have the (SELECT [View] FROM [views] WHERE id = @id) equal to a variable, then I could write the SQL query in VB and execute it and the variable would become part of the string and therefore execute correctly.

I'm newer to VB, but here's what I have so far:

Dim cn As SqlConnection = New SqlConnection("Data Source=Server1;" & _
                                                 "Initial Catalog=DB1;" & _
                                                 "Integrated Security=SSPI")
cn.Open()

Dim cmd As New sqlCommand("SELECT max(id) FROM orientation_view_experiment;", cn)
vID = cmd.ExecuteNonQuery()

Do While vID > 0
    Dim cmd2 As New sqlCommand("SELECT [View] FROM [views] WHERE id ='" + vID + "'"
    vViewType = cmd2.ExecuteNonQuery()

    Dim cmd3 As New sqlCommand("UPDATE a
    SET a.'" + vViewType + "' = '1' & _
    FROM [summary] a & _
    INNER JOIN [TeamImage] b & _
    ON a.[Part_Number] = b.[PartNum] & _

    WHERE b.[View] = '" + vViewType + "';"
    cmd3.ExecuteNonQuery()
vID = vID - 1
Loop

cn.Close()

I hope some of that made sense, but I'm kind of lost at this point. I feel like I know what I need the SQL to do, but can't quite figure out how to make the computer/programs submit to my will and just do what I need it to do.

Thank you for any help/direction you can give.

4

3 回答 3

2

您的代码是错误的,因为您坚持使用ExecuteNonQueryforSELECT语句。ExecuteNonQuery不返回选定的行,而只返回受 INSERT/DELETE/UPDATE 查询影响的行数(我认为对于 SELECT 它总是返回零)

您需要的是ExecuteScalar来获取 MAX 值和 VIEW 值,因为当您希望从 SQL 语句中仅获取第一行的第一个字段时,ExecuteScalar 是最佳选择

Dim cmd As New sqlCommand("SELECT max(id) FROM orientation_view_experiment;", cn)
vID = Convert.ToInt32(cmd.ExecuteScalar())

Do While vID > 0
    Dim cmd2 As New sqlCommand("SELECT [View] FROM [views] WHERE id =" + vID.ToString()
    Dim result = cmd2.ExecuteScalar()
    if Not string.IsNullOrEmpty(result)) Then
       vViewType = result.ToString()

       Dim cmd3 As New sqlCommand("UPDATE a SET a.[" + vViewType + "] = '1' " & _
                                  "FROM [summary] a " & _
                                  "INNER JOIN [TeamImage] b " & _
                                  "ON a.[Part_Number] = b.[PartNum] " & _
                                  "WHERE b.[View] = @vType"
       cmd3.Parameters.AddWithValue("@vType", vViewType)
       cmd3.ExecuteNonQuery()
    End If
Loop

您的代码的最后一部分对我来说不是很清楚,但是您可以在 table 中的列名周围使用几个方括号,summary并为 table 中的 View 字段使用一个参数TeamImage

作为最后的建议,请确保您的最终用户不能直接修改表 TeamImage 中的 View 列,因为这样的字符串连接可能会导致Sql Injection攻击

于 2013-10-15T15:01:16.550 回答
0

您是否尝试过替换'" + vViewType + "'[" + vViewType + "]... 换句话说,使用方括号来分隔列名而不是用于分隔字符串文字的单引号?

另外,我鼓励在调试器中停下来,检查您在 cmd3 中生成的命令并尝试直接执行它。它可能会帮助您识别其他类似的问题,例如 vViewType 为您提供记录计数而不是[View]列中的实际值。

于 2013-10-15T15:02:13.957 回答
0

对 a 的不同方法进行一些研究command。当您调用时ExecuteNonQuery,这将返回受影响的记录数。我认为你想要ExecuteScalar作为你的cmdcmd2方法,这样你就可以从数据库中获取一个值。

于 2013-10-15T14:58:27.590 回答