C# 如果为空,如何返回0,然后导入到datagridview
我有这样的sql数据C# 如果为空,如何返回0,然后导入到datagridview,c#,sql,sql-server,datagridview,C#,Sql,Sql Server,Datagridview,我有这样的sql数据 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";
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 (Ngay.Ngay=@Ngay 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 KhuVuc.Ten=@Ten)))))))))))))";
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();
}
该代码的结果是:
12米
2.3米
3m
6.6米
76米
我需要修正什么才能从1到7完整地显示它。在这种情况下,我认为最简单的方法是每次检查当前值是否是前一个值+1的总和
if (current != (previous + 1)
{
//make new 4 0m or something
}
else
{
//continue reading
}
尝试创建一个临时表,该表将包含缺少的值,然后连接主表和临时表 这就是它的样子
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
您可以使用理货台主..spt_值来处理序列中缺少的项目 您可以通过左键连接到表并合并值来实现这一点 比如说
CREATE TABLE ATable
([Number] int, [value] varchar(2))
;
INSERT INTO ATable
([Number], [value])
VALUES
(1, '2m'),
(2, '3m'),
(3, '3m'),
(6, '6m'),
(7, '6m')
;
这个
您的表结构是什么???查询是工作的,但我希望它显示数据中没有的值。这意味着如果in数据不存在,我想向datagridview添加值0。1-7是否存在于您的数据库中1 2m 2 3m 3 3m 4 null 5 null 6 6m 7 6m您可以得到如下结果:adapter.Filltbl;dataGridView2.DataSource=tbl;在一个如此难以理解的循环中是没有意义的。也许我不够聪明,无法理解。但是非常感谢。您不需要临时表,因为SQL Server提供master..sptvalues也可以合并,或者经常使用ISNULL来代替NULL时的大小写
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 |