2016-02-25 75 views
0

我想從存儲過程中加載下拉列表,我沒有任何運氣。我對編碼很陌生,所以我甚至不能給你一個有教養的猜測,即我做錯了什麼。任何援助非常感謝。從存儲過程的負載下拉

Private Sub LoadDropDowns() 
    Dim drData As SqlDataReader 
    Dim liItem As ListItem 
    lblError.Text = "" 
    lblError2.Text = lblError.Text 
    Dim aNames As ArrayList 
    Dim sSQL As String = "" 

    Try 
     ddlEmployee.Items.Clear() 
     liItem = New ListItem 
     liItem.Value = drData("StaffID") 
     liItem.Text = drData("Fullname") 
     ddlEmployee.Items.Add(liItem) 

     Try 

      drData = ExecuteSQLReaderProc("dbo.FetchCWSSocialWorkers", "phemployee") 

     Catch ex As Exception 
      lblError.Text = ex.Message 
     End Try 
     While drData.Read 
      liItem = New ListItem 
      liItem.Value = drData("StaffID") 
      liItem.Text = drData("Fullname") 
      ddlEmployee.Items.Add(liItem) 

     End While 
     ddlEmployee.SelectedIndex = 0 
    Catch myException As Exception 
     lblError.Text = myException.Message 
    Finally 
    End Try 

回答

0

以下代碼可以執行SQL存儲過程並將結果保存到工作表中的範圍。

Sub ImportStoreProcedure() 

    Dim conn As New ADODB.Connection 
    Dim rst As Object 
    Set rst = CreateObject("ADODB.Recordset") 
    Dim StoredProceedure As String 
    StoredProceedure = "Stored_Proceedure_Name" 

     conn.Open "Provider=SQLOLEDB;Data Source=MACHINE\SERVERNAME;Initial Catalog=CATALOGNAME;User ID=xx; Password=xxxxxxx; " 

     rst.Open StoredProceedure, conn 
     Sheets(1).Range("A2").CopyFromRecordset rst 

     conn.Close 
     Set conn = Nothing 
End Sub