2013-10-08 38 views
0

我想創建一個腳本來整理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 
+1

和錯誤消息是什麼? –

+4

你**不能**在作爲參數的'UPDATE'語句中傳遞列的名稱.....如果你絕對必須這樣做,你需要使用*動態SQL *(將你的SQL語句構建爲字符串,然後在存儲過程中執行它) –

+1

該代碼不會拋出錯誤消息,因爲它只是更新變量。要獲得動態列名,您需要使用動態SQL(或複雜的case語句),但在這種情況下動態SQL更好。 –

回答

1

,因爲它簡單地更新變量,代碼不會拋出一個錯誤消息。要獲得動態列名,您需要使用動態SQL(或複雜的case語句),但在這種情況下動態SQL更好。

DECLARE @sql nvarchar(max) = ''; 

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

EXEC sp_executesql @sql 

希望這有助於:)

+0

請記住,這是開放的SQL注入,所以要小心。 –