Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 存储过程变量(我认为存在代码错误)_Asp.net_Sql_Sql Server_Vb.net - Fatal编程技术网

Asp.net 存储过程变量(我认为存在代码错误)

Asp.net 存储过程变量(我认为存在代码错误),asp.net,sql,sql-server,vb.net,Asp.net,Sql,Sql Server,Vb.net,我正在尝试创建一个脚本来整理SQL Server中的所有列和行(我将做的不仅仅是修剪,但一旦它起作用,我就可以插入我已经拥有的应用程序代码) 我想我在那里,但它似乎没有更新-也许存储过程是错误的?我相信错误就在这里我环顾了存储过程中的变量,认为它对我来说是正确的 Insert statements for procedure here UPDATE systemUsers SET @ColumnName = @Update WHERE ID = @ID 完整脚本 Protected Su

我正在尝试创建一个脚本来整理SQL Server中的所有列和行(我将做的不仅仅是修剪,但一旦它起作用,我就可以插入我已经拥有的应用程序代码)

我想我在那里,但它似乎没有更新-也许存储过程是错误的?我相信错误就在这里我环顾了存储过程中的变量,认为它对我来说是正确的

Insert statements for procedure here

UPDATE systemUsers 
SET @ColumnName = @Update
WHERE ID = @ID 
完整脚本

Protected Sub btnTest_Click(sender As Object, e As System.EventArgs) Handles btnTest.Click

        'Select the column names
        Dim cn As SqlConnection = New SqlConnection()
        Dim cmd As SqlCommand = New SqlCommand()
        Dim dr As SqlDataReader
        Dim i As Integer = 0
        cn.ConnectionString = ConfigurationManager.ConnectionStrings("mySQLConnectionString").ConnectionString
        cmd.Connection = cn
        ' this gets the colum name
        cmd.CommandText = "select COLUMN_NAME FROM INFORMATION_SCHEMA.Columns where TABLE_NAME = 'systemUsers'"

        'Open the connection to the database
        cn.Open()
        ' Execute the sql.
        dr = cmd.ExecuteReader()
        ' Read all of the rows generated by the command (in this case only one row).

        CheckBoxList1.Items.Clear() 'remove all items for the new list

        Do While dr.Read()

            i = i + 1
            Session.Item("ColumnName" & i) = dr.Item("COLUMN_NAME").ToString()
        Loop
        ' Close your connection to the DB.
        dr.Close()
        cn.Close()

        Dim j As Integer = 1
        For j = 1 to i
            cn.ConnectionString = ConfigurationManager.ConnectionStrings("mySQLConnectionString").ConnectionString
            cmd.Connection = cn
            cmd.CommandText = "Select * From [systemUsers]"

            'Open the connection to the database
            cn.Open()
            ' Execute the sql.
            dr = cmd.ExecuteReader()
            ' Read all of the rows generated by the cmd (in this case only one row).
            Dim vName As String = ""
            If vName = "ID" Then
                'skip as ID should never be edited!!
            Else

                Do While dr.Read()

                    vName = Session.Item("ColumnName" & j).ToString ' put into vName for Stored Procedure

                    Dim sConnString As String = System.Web.Configuration.WebConfigurationManager.ConnectionStrings("mySQLConnectionString").ConnectionString
                    Dim dsNames As SqlDataSource

                    dsNames = New SqlDataSource
                    dsNames.ConnectionString = sConnString
                    Dim sSQL As String
                    sSQL = "SPTidy"

                    dsNames.UpdateCommand = sSQL
                    dsNames.UpdateCommandType = SqlDataSourceCommandType.StoredProcedure
                    dsNames.UpdateParameters.Clear()
                    dsNames.UpdateParameters.Add("ID", dr.Item("ID").ToString())
                    dsNames.UpdateParameters.Add("Update", Trim(dr.Item(vName).ToString()))
                    dsNames.UpdateParameters.Add("ColumnName", vName)
                    dsNames.Update()
                    dsNames = Nothing

                Loop
            End If

            j = j + 1

            ' Close your connection to the DB.
            dr.Close()
            cn.Close()

        Next

    End Sub
存储过程:

create PROCEDURE [dbo].[SPTidy] 
   @ID bigint,
   @Update nvarchar(max),
   @ColumnName nvarchar(max)
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    UPDATE systemUsers 
    SET @ColumnName = @Update
    WHERE ID = @ID
END

该代码不会抛出错误消息,因为它只是更新变量。要获得动态列名,您需要使用动态SQL(或复杂的case语句),但在这种情况下,动态SQL更好

DECLARE @sql nvarchar(max) = '';

SET @sql = N'UPDATE systemUsers SET ' + @ColumnName + N' = ' + @Update + N' WHERE ID=' + @ID

EXEC sp_executesql @sql

希望这有帮助:)

错误消息是?您不能将
UPDATE
语句中的列名作为参数传递。。。。。如果绝对必须这样做,则需要使用动态SQL(将SQL语句构建为字符串,然后在存储过程中执行),这样代码不会抛出错误消息,因为它只是更新变量。要获得动态列名,您需要使用动态SQL(或复杂的case语句),但在这种情况下,动态SQL更好。请记住,这是对SQL注入开放的,所以要小心。