2013-03-12 57 views
2

我在SQL Server中獲得了一個存儲過程我創建了一些內部連接,現在我將如何使用該存儲過程填充我的數據網格。使用SQL Server存儲過程填充datagrid1.view

這裏是我的代碼不工作

Dim cmd As New SqlCommand 
     Dim reader As SqlDataReader 

     cmd.CommandText = "OfficeEquipmentProfile" 
     cmd.CommandType = CommandType.StoredProcedure 
     cmd.Connection = sqlconn 

     sqlconn.Open() 



     sAdapter = New SqlDataAdapter(cmd) 
     sBuilder = New SqlCommandBuilder(sAdapter) 
     sDs = New DataSet 
     sAdapter.Fill(sDs, "tblOfficeEquipmentProfile") 
     sAdapter.Fill(sDs, "tblDepartment") 
     sAdapter.Fill(sDs, "tblLocation") 
     sAdapter.Fill(sDs, "tblOfficeEquipmentCategory") 
     sAdapter.Fill(sDs, "tblApplication") 
     sAdapter.Fill(sDs, "tblApplicationLicense") 
     sAdapter.Fill(sDs, "tblEquipmentApplication") 
     sAdapter.Fill(sDs, "tblOfficeEquipmentBrand") 
     sAdapter.Fill(sDs, "tblOfficeEquipmentModel") 
     sAdapter.Fill(sDs, "tblOfficeEquipmentServiceOrder") 
     sAdapter.Fill(sDs, "tblOfficeEquipmentPMplan") 


     sTable = sDs.Tables("tblOfficeEquipmentProfile") 
     sTable = sDs.Tables("tblDepartment") 
     sTable = sDs.Tables("tblLocation") 
     sTable = sDs.Tables("tblOfficeEquipmentCategory") 
     sTable = sDs.Tables("tblApplication") 
     sTable = sDs.Tables("tblApplicationLicense") 
     sTable = sDs.Tables("tblEquipmentApplication") 
     sTable = sDs.Tables("tblOfficeEquipmentBrand") 
     sTable = sDs.Tables("tblOfficeEquipmentServiceOrder") 
     sTable = sDs.Tables("tblOfficeEquipmentPMplan") 

     DataGrid1.DataSource = sDs.Tables("tblOfficeEquipmentProfile, tblDepartment, tblLocation, tblOfficeEquipmentCategory, tblApplication,tblApplicationLicense, tblEquipmentApplication, tblOfficeEquipmentBrand, tblOfficeEquipmentServiceOrder,tblEquipmentPMplan") 
     DataGrid1.ReadOnly = True 
     'Button1.Enabled = False 
     'DataGrid1.SelectionMode = DataGridViewSelectionMode.FullRowSelect 


     reader = cmd.ExecuteReader() 
     sqlconn.Close() 

我只是想從數據庫中顯示的記錄

+1

你會得到什麼錯誤? – DevelopmentIsMyPassion 2013-03-12 07:59:42

+0

它沒有返回錯誤,同時沒有顯示來自數據庫的任何記錄 – ivandinglasan 2013-03-12 08:02:53

+0

請顯示您的存儲過程。我不能看到你正在調用的參數cmd.parameter – DevelopmentIsMyPassion 2013-03-12 08:13:21

回答

1

當您返回從不同的表中的列,而不是多個表,那麼你只需要此代碼。

Dim Command As SqlCommand = New SqlCommand() 
Command.Connection = Connection 
Command.CommandText = "OfficeEquipmentProfile" 
Command.CommandType = CommandType.StoredProcedure 

Dim sAdapter As SqlDataAdapter = New SqlDataAdapter(Command) 

Dim DataSet As DataSet = New DataSet(Command.CommandText) 

sAdapter.Fill(DataSet) 
DataGrid1.DataSource = DataSet.Tables(0) 
+0

DataGrid1.DataBind()不是system.windows.forms.datagrid IM的成員用vb.net 2003 – ivandinglasan 2013-03-13 00:41:38

+0

@ivandinglasan請SEEE更新的代碼,現在嘗試 – DevelopmentIsMyPassion 2013-03-13 07:08:01

+0

@APC,AshReva非常感謝!你們都幫助很大! – ivandinglasan 2013-03-13 08:30:28

0

嘗試使用此代碼

Dim cmd As New SqlCommand 
cmd.CommandText = "OfficeEquipmentProfile" 
cmd.CommandType = CommandType.StoredProcedure 
cmd.Connection = sqlconn 
sqlconn.Open() 

sAdapter = New SqlDataAdapter(cmd) 
sBuilder = New SqlCommandBuilder(sAdapter) 
sDs = New DataSet 
sAdapter.Fill(sDs) 
DataGrid1.DataSource = sDs 

的StoredProcedure的回報一個或多個表,但不需要爲每個表調用填充。一個就足夠了。然後分配整個數據集的數據源,或者如果您返回多個表,需要一個特定的表

DataGrid1.DataSource = sDs 
DataGrid1.DataMember = "tablename" 
0
Try 
     If con.State = ConnectionState.Open Then con.Close() 
     con.Open() 
     Dim dset As New DataSet 
     Dim dbind As New BindingSource 
     Dim strquery As String 
     strquery = "SELECT prod_code, prod_no, prod_suffix, prod_lvl, customer, model, family, company_code, company_name, company_address, running_no, template_code FROM products_tbl WHERE template_code = 'n'and company_code = 'YTPL' and prod_no = '" & txt_product.Text & "' and prod_suffix = '" & txt_suffix.Text & "' and prod_lvl = '" & txt_level.Text & "'" 
     Dim adap As New SqlDataAdapter(strquery, con) 
     dset = New DataSet 
     adap.Fill(dset) 
     dbind.DataSource = dset.Tables(0) 
     dg.DataSource = dbind 
    Catch ex As Exception 
     MsgBox("Trace No 3: System Error or Data Error!" + Chr(13) + ex.Message + Chr(13) + "Please Contact Your System Administrator!", vbInformation, "Message") 
    End Try