大家好我需要幫助從sql數據庫獲取數據到datagrid。 我想算不同的值,並把它放入DataGrid列,這也是這樣一個獨特的價值: - !SQL Server服務器在vb.net中不同的關鍵字
| Partyname | Turbo | Truck | Total | +-----------+-------+-------+-------+ | Abc | 33 | 3 | 36 | | xyz | 30 | 10 | 40 | | qwe | 2 | 4 | 6 |
但是我得到的是: -
| Partyname | Turbo | Truck | Total | +-----------+-------+-------+-------+ | Abc | | | | | xyz | | | | | qwe | | | |
我的代碼是: -
Dim sCommand, cmd As SQLCommand
Dim sadp As SQLDataAdapter
Dim sds As New DataSet
Dim stable As New DataTable
Private Sub OK_Button_Click_1(sender As System.Object, e As System.EventArgs) Handles OK_Button.Click
Dim sts As String = ""
Dim dt, dt2 As String
dt = (Date_from.Value.Date).ToShortDateString + " " + (DateTimeInput1.Text)
dt2 = (Date_to.Value.Date).ToShortDateString + " " + (DateTimeInput2.Text)
Try
Credit_report_daily.Show()
Dim sql As String
sql = "SELECT Distinct Party_name FROM Weightment Where Cash_Cr='Credit' AND (Weigh_date Between @dt AND @dt2) Group by Party_name"
Using com As New SqlCommand(sql, Con)
Con.Open()
com.Parameters.AddWithValue("@dt", DateTime.Parse(dt))
com.Parameters.AddWithValue("@dt2", DateTime.Parse(dt2))
sadp = New SqlDataAdapter(com)
sds = New DataSet()
sadp.Fill(sds, "General")
stable = sds.Tables("General")
Credit_report_daily.DataGridView1.DataSource = sds.Tables("General")
Dim cmd As New SqlCommand
cmd.Connection = Con
cmd.CommandText = "SELECT Distinct Vehicle_Type FROM Weightment Where Cash_Cr='Credit' AND (Weigh_date Between @dt AND @dt2) Group by Vehicle_Type"
cmd.Parameters.AddWithValue("@dt", DateTime.Parse(dt))
cmd.Parameters.AddWithValue("@dt2", DateTime.Parse(dt2))
Dim da As SqlDataReader = cmd.ExecuteReader
While da.Read
Credit_report_daily.DataGridView1.Columns.Add(da.Item(0), da.Item(0))
End While
da.Close()
Con.Close()
End Using
Catch ex As Exception
MsgBox(ex.ToString)
End Try
Me.Close()
End Sub
你可以幫我嗎??????
我被計數獲取列其列VEHICLE_TYPE 像這多少不同的值是: -
Dim cmd As New SqlCommand
cmd.Connection = Con
cmd.CommandText = "SELECT Distinct Vehicle_Type FROM Weightment Where Cash_Cr='Credit' AND (Weigh_date Between @dt AND @dt2) Group by Vehicle_Type"
cmd.Parameters.AddWithValue("@dt", DateTime.Parse(dt))
cmd.Parameters.AddWithValue("@dt2", DateTime.Parse(dt2))
Dim da As SqlDataReader = cmd.ExecuteReader
While da.Read
Credit_report_daily.DataGridView1.Columns.Add(da.Item(0), da.Item(0))
End While
da.Close()
Con.Close()
我覺得Turbo,Truck,Total是你的表的列,我是對嗎?那麼你在哪裏從數據庫中獲取這些列值?無論如何,你可以把你的數據庫放在這裏。我認爲這對Ans很有幫助。 – 2014-11-24 10:41:01
指定輸出列和它應該是什麼樣子? – 2014-11-24 10:41:45
我通過計算不同的值來從數據庫中獲取列,如下所示: - 'cmd.CommandText =「選擇不同的Vehicle_Type FROM權重其中Cash_Cr ='Credit'AND(在@dt和@ dt2之間權衡日期)Group by Vehicle_Type」' – sneha768 2014-11-24 11:33:07