2013-04-17 71 views
-1

我有一個SQL數據這樣如何返回0,如果null,則導入到datagridview的

1 2m 
2 3m 
3 3m 
6 6m 
7 6m 

我要出口,並顯示到datagridview的。

1 2m 
2 3m 
3 3m 
4 0m 
5 0m 
6 6m 
7 6m 

這裏是我的代碼:

private void laygio(string tenstore, string tenxuat) 
{ 
    string conn = "Data Source=USER-PC;Initial Catalog=NCKHmoi;Integrated Security=True"; 
    SqlConnection connect = new SqlConnection(conn); 
    SqlCommand command = new SqlCommand(); 
    command.Connection = connect; 
    connect.Open(); 
    int stday = Convert.ToInt32(dst.Text); 
    int stmonth = Convert.ToInt32(mst.Text); 
    int styear = Convert.ToInt32(yst.Text); 
    int sthour = Convert.ToInt32(hst.Text); 
    int stminute = 0; 
    int stsecond = 0; 
    int eday = Convert.ToInt32(ded.Text); 
    int emonth = Convert.ToInt32(med.Text); 
    int eyear = Convert.ToInt32(yed.Text); 
    int ehour = Convert.ToInt32(hed.Text); 
    int eminute = 0; 
    int esecond = 0; 
    DateTime startday = new DateTime(styear, stmonth, stday, sthour, stminute, stsecond); 
    DateTime endday = new DateTime(eyear, emonth, eday, ehour, eminute, esecond); 
    DataTable tbl = new DataTable(); 
    DataColumn Col = new DataColumn("Thời gian", typeof(int)); 
    tbl.Columns.Add(Col); 
    SqlDataAdapter adapter = new SqlDataAdapter(); 
    int i = 1; 
    for (DateTime xday = startday; xday <= endday; xday += TimeSpan.FromHours(1)) 
    { 
     int ngay = Convert.ToInt32(xday.Day.ToString()); 
     int thang = Convert.ToInt32(xday.Month.ToString()); 
     int nam = Convert.ToInt32(xday.Year.ToString()); 
     int gio = Convert.ToInt32(xday.Hour.ToString()); 
     command.CommandType = CommandType.Text; 
     command.CommandText = @"Select SoLieuGio.LLNuoc from SoLieuGio where SoLieuGio.GioID= (select Gio.GioID from Gio where (Gio.Gio = @Gio and Gio.NgayID= (select Ngay.NgayID from Ngay where ([email protected] and Ngay.ThangID= (select Thang.ThangID from Thang where (Thang.Thang = @Thang and Thang.NamID=(select Nam.NamID from Nam where (Nam.Nam = @Nam and Nam.TramID=(select Tram.TramID from Tram Where (Tram.TenTram like @TenTram and Tram.TinhID=(select Tinh.TinhID from Tinh where (Tinh.TenTinh like @TenTinh and Tinh.KhuVucID=(select KhuVuc.KhuVucID from KhuVuc where [email protected])))))))))))))"; 
     command.Parameters.Add("@Gio", SqlDbType.BigInt).Value = gio; 
     command.Parameters.Add("@Ngay", SqlDbType.BigInt).Value = ngay; 
     command.Parameters.Add("@Thang", SqlDbType.BigInt).Value = thang; 
     command.Parameters.Add("@Nam", SqlDbType.BigInt).Value = nam; 
     command.Parameters.Add("@Ten", SqlDbType.NVarChar, 50).Value = "Đồng Bằng Bắc Bộ"; 
     command.Parameters.Add("@TenTinh", SqlDbType.NVarChar, 50).Value = TinhComboBoxEx.Text; 
     command.Parameters.Add("@TenTram", SqlDbType.NVarChar, 50).Value = TramComboBoxEx.Text; 
     adapter.SelectCommand = command; 
     adapter.Fill(tbl); 
     dataGridView2.DataSource = tbl;    
     command.Parameters.Clear(); 
    } 
    command.Dispose(); 
    connect.Close(); 
} 

代碼的結果是:

1 2公尺 23米 33米 66米 76米

什麼需要我修復完全顯示它從1到7.

+0

什麼是你的表結構??? –

+0

查詢是工作,但我希望它顯示不在數據中的值。這意味着我想添加到datagridview值0如果在數據沒有。 –

+0

1-7是存在於數據庫中或不 –

回答

2

您可以使用符合表master..spt_values處理您的系列丟失物品。

您可以通過左側的接合部到表中做到這一點,凝聚你的價值觀。

例如

CREATE TABLE ATable 
    ([Number] int, [value] varchar(2)) 
; 

INSERT INTO ATable 
    ([Number], [value]) 
VALUES 
    (1, '2m'), 
    (2, '3m'), 
    (3, '3m'), 
    (6, '6m'), 
    (7, '6m') 
; 

SELECT 
    v.Number, 
    COALESCE(ATable.Value, '0m') Value 
FROM 
    master..spt_values v 
    LEFT JOIN ATable 
    ON v.Number = ATable.Number 
WHERE 
    v.Type = 'P' 
    and v.Number > 0 and v.Number < 8 

會產生這個值

| NUMBER | VALUE | 
------------------ 
|  1 | 2m | 
|  2 | 3m | 
|  3 | 3m | 
|  4 | 0m | 
|  5 | 0m | 
|  6 | 6m | 
|  7 | 6m | 

DEMO

+0

非常感謝。我會試試看。 –

0

在這種情況下,我認爲最簡單的辦法是檢查每次如果當前值是前值的總和+ 1

if (current != (previous + 1) 
{ 
    //make new 4 0m or something 
} 
else 
{ 
    //continue reading 
} 
0

嘗試創建一個臨時表,將舉行缺失值,然後加入主表和臨時表。

這是怎麼可能看起來像

create table #temp1 
(
    ID int identity(1,1), 
    M varchar(4) 
) 
declare @count int 
SET @count = 0 

declare @max int 
SET @max = 20 

while @count < @max 
begin 
insert into #temp1 (M) values ('0m') 
SET @count = @count + 1 
end 

select * from #temp1 

select (case M1.ID when null then T1.ID else M1.ID) as ID, 
(case M1.M when null then T1.M else M1.ID) as TXT 
from MainTable M1 
join #temp1 T1 
order by ID 
+0

很難理解它。也許我不夠聰明。但。非常感謝你。 –

+0

因爲SQL Server提供了'master..sptvalues',所以你不需要臨時表,而且經常使用'COALESCE'或'ISNULL'來代替'CASE WHEN NULL' –

相關問題