mysql vb.net

时间:2016-11-22 20:08:09

标签: mysql vb.net datagridview datetimepicker

您好Stackoverflow管理员和所有成员,我在使用动态列按日期生成datagridview时遇到问题。

我有一些酒店预订表:

CREATE TABLE IF NOT EXISTS `tp_tblmsroom` (
  `KdRoom` varchar(10) COLLATE latin1_general_ci NOT NULL DEFAULT '',
  `NmRoom` varchar(100) COLLATE latin1_general_ci NOT NULL,
  `KdRoomType` varchar(10) COLLATE latin1_general_ci NOT NULL
PRIMARY KEY (`KdRoom`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;

INSERT INTO `tp_tblmsroom` (`KdRoom`, `NmRoom`, `KdRoomType`) VALUES
  ('1001', 'Lantai 1 No. 001', 'SNG-SUP'),
  ('1002', 'Lantai 1 No. 002', 'SNG-SUP'),
  ('2001', 'Lantai 2 No. 001', 'SNG-DLX'),
  ('2002', 'Lantai 2 No. 002', 'SNG-DLX'),
  ('3001', 'Lantai 3 No. 001', 'TWN-JRS'),
  ('3002', 'Lantai 3 No. 002', 'TWN-JRS'),
  ('3003', 'Lantai 3 No. 003', 'TWN-JRS'),
  ('4001', 'Lantai 4 No. 001', 'SNG-STE'),
  ('5001', 'Lantai 5 No. 001', 'TWN-PRE'),
  ('6001', 'Lantai 6 No. 001', 'SNG-STD'),
  ('6002', 'Lantai 6 No. 002', 'SNG-STD'),
  ('6003', 'Lantai 6 No. 003', 'SNG-STD'),
  ('6004', 'Lantai 6 No. 004', 'SNG-STD'),
  ('6005', 'Lantai 6 No. 005', 'SNG-STD'),
  ('6006', 'Lantai 6 No. 006', 'TWN-STD'),
  ('6007', 'Lantai 6 No. 007', 'TWN-STD'),
  ('6008', 'Lantai 6 No. 008', 'TWN-STD'),
  ('6009', 'Lantai 6 No. 009', 'TWN-STD');

CREATE TABLE IF NOT EXISTS `tp_tblstroomstatus` (
  `KdRoom` varchar(10) COLLATE latin1_general_ci NOT NULL,
  `DateCheck` datetime NOT NULL,
  `KdRoomStatus` varchar(10) COLLATE latin1_general_ci NOT NULL,
  `KdTransaksi` varchar(21) COLLATE latin1_general_ci NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;

INSERT INTO `tp_tblstroomstatus` (`KdRoom`, `DateCheck`, `KdRoomStatus`) VALUES
  ('6006', '2016-11-22 14:00:00', 'BO'),
  ('6006', '2016-11-23 14:00:00', 'BO'),
  ('6006', '2016-11-23 12:00:00', 'BO'),
  ('6006', '2016-11-24 12:00:00', 'BO'),
  ('6007', '2016-11-22 14:00:00', 'BO'),
  ('6007', '2016-11-23 14:00:00', 'BO'),
  ('6007', '2016-11-24 14:00:00', 'BO'),
  ('6007', '2016-11-23 12:00:00', 'BO'),
  ('6007', '2016-11-24 12:00:00', 'BO'),
  ('6007', '2016-11-25 12:00:00', 'BO');  

我尝试创建一个查询,以便在我调用2个datetimepicker和绑定到VB.NET中的datagridview时使日期动态化。

SELECT `tp_tblmsroom`.`KdRoom`,
  MAX(IF(`tp_tblstroomstatus`.`DateCheck` = '2016-11-20 14:00:00', `tp_tblstroomstatus`.`KdRoomStatus`, '')) AS `2016-11-20 14:00:00`,
  MAX(IF(`tp_tblstroomstatus`.`DateCheck` = '2016-11-21 12:00:00', `tp_tblstroomstatus`.`KdRoomStatus`, '')) AS `2016-11-21 12:00:00`,
  MAX(IF(`tp_tblstroomstatus`.`DateCheck` = '2016-11-21 14:00:00', `tp_tblstroomstatus`.`KdRoomStatus`, '')) AS `2016-11-21 14:00:00`,
  MAX(IF(`tp_tblstroomstatus`.`DateCheck` = '2016-11-22 12:00:00', `tp_tblstroomstatus`.`KdRoomStatus`, '')) AS `2016-11-22 12:00:00`,
  MAX(IF(`tp_tblstroomstatus`.`DateCheck` = '2016-11-22 14:00:00', `tp_tblstroomstatus`.`KdRoomStatus`, '')) AS `2016-11-22 14:00:00`,
  MAX(IF(`tp_tblstroomstatus`.`DateCheck` = '2016-11-23 12:00:00', `tp_tblstroomstatus`.`KdRoomStatus`, '')) AS `2016-11-23 12:00:00`,
  MAX(IF(`tp_tblstroomstatus`.`DateCheck` = '2016-11-23 14:00:00', `tp_tblstroomstatus`.`KdRoomStatus`, '')) AS `2016-11-23 14:00:00`,
  MAX(IF(`tp_tblstroomstatus`.`DateCheck` = '2016-11-24 12:00:00', `tp_tblstroomstatus`.`KdRoomStatus`, '')) AS `2016-11-24 12:00:00`,
  MAX(IF(`tp_tblstroomstatus`.`DateCheck` = '2016-11-24 14:00:00', `tp_tblstroomstatus`.`KdRoomStatus`, '')) AS `2016-11-24 14:00:00`,
  MAX(IF(`tp_tblstroomstatus`.`DateCheck` = '2016-11-25 12:00:00', `tp_tblstroomstatus`.`KdRoomStatus`, '')) AS `2016-11-25 12:00:00`,
  MAX(IF(`tp_tblstroomstatus`.`DateCheck` = '2016-11-25 14:00:00', `tp_tblstroomstatus`.`KdRoomStatus`, '')) AS `2016-11-25 14:00:00`,
  MAX(IF(`tp_tblstroomstatus`.`DateCheck` = '2016-11-26 12:00:00', `tp_tblstroomstatus`.`KdRoomStatus`, '')) AS `2016-11-26 12:00:00`
FROM `tp_tblstroomstatus`
  RIGHT OUTER JOIN `tp_tblmsroom` ON (`tp_tblstroomstatus`.`KdRoom` = `tp_tblmsroom`.`KdRoom`)
GROUP BY `tp_tblmsroom`.`KdRoom`  

然后我尝试将其转换为:

SET @sql = NULL;
SELECT GROUP_CONCAT
  (DISTINCT CONCAT(
     'MAX(IF(`tp_tblstroomstatus`.`DateCheck` = ''',
  `tp_tblstroomstatus`.`DateCheck`,
  ''', `tp_tblstroomstatus`.`KdRoomStatus`, '')) AS ',
  `tp_tblstroomstatus`.`DateCheck`
)
) INTO @sql
FROM `tp_tblstroomstatus`
   RIGHT OUTER JOIN `tp_tblmsroom` ON (`tp_tblstroomstatus`.`KdRoom` = `tp_tblmsroom`.`KdRoom`);
SET @sql = CONCAT('SELECT `tp_tblmsroom`.`KdRoom`, ', @sql, ' FROM `tp_tblstroomstatus`
RIGHT OUTER JOIN `tp_tblmsroom` ON (`tp_tblstroomstatus`.`KdRoom` = `tp_tblmsroom`.`KdRoom`) GROUP BY `tp_tblmsroom`.`KdRoom`');  

但是什么时候尝试运行MySQL代码,我得到了这个错误。

you have an error in your SQL syntax....'SELECT GROUP_CONCAT(DISTICT... 

有关其他信息,我尝试在datetimepicker之间使用生成的新列循环datagridview。这是我的VB.NET代码:

Dim pDateCI As Date = dtpStartRoomRack.Text
Dim pDateCO As Date = dtpEndRoomRack.Text
Dim DayCount As Integer = DateDiff(DateInterval.Day, pDateCI, pDateCO)
  For i = 0 To DayCount - 1
    .Columns.Add(i, (Format(pDateCI, "yyyy/MM/dd") & " " & Format(pTimeCI, "HH:mm:ss"))) 'format pDateCI date use for call mysql data in checkdate column like 2016-11-21 14:00:00'
    .Columns.Add(i, (Format(pDateCI.AddDays(1), "yyyy/MM/dd") & " " & Format(pTimeCO, "HH:mm:ss"))) 'format pDateCO date use for call mysql data in checkdate column like 2016-11-22 12:00:00'
    pDateCI = pDateCI.AddDays(1)
  Next  

我该如何解决我的问题?在此先感谢:)

1 个答案:

答案 0 :(得分:0)

温迪,我要在这里猜一猜。我认为你的查询很好,你有一个休息,你不应该有一个。查看下面的第二行并将其保存在一行中,然后尝试运行查询。

SET @sql = NULL;
SELECT GROUP_CONCAT(DISTINCT CONCAT(
     'MAX(IF(`tp_tblstroomstatus`.`DateCheck` = ''',
  `tp_tblstroomstatus`.`DateCheck`,
  ''', `tp_tblstroomstatus`.`KdRoomStatus`, '')) AS ',
  `tp_tblstroomstatus`.`DateCheck`
)
) INTO @sql
FROM `tp_tblstroomstatus`
   RIGHT OUTER JOIN `tp_tblmsroom` ON (`tp_tblstroomstatus`.`KdRoom` = `tp_tblmsroom`.`KdRoom`);
SET @sql = CONCAT('SELECT `tp_tblmsroom`.`KdRoom`, ', @sql, ' FROM `tp_tblstroomstatus`
RIGHT OUTER JOIN `tp_tblmsroom` ON (`tp_tblstroomstatus`.`KdRoom` = `tp_tblmsroom`.`KdRoom`) GROUP BY `tp_tblmsroom`.`KdRoom`');