比如数据库中有一张表里面 (入库时间)字段ISDATE 是 2009-08-11 10:12:15.340
在FORM窗体中有个combobox1,textBox1,Button和DataGridView。开始选择combobox1.text==入库时间,在textBox1中输入2009-08后可以查询到该条数据库中记录。
主要BUTTON中代码如下:
if (combobox1.Text.Trim() == "入库日期")
{
string P_str_dtime = txtLKWord.Text.Trim();
DataSet myds = datacon.getds("select ISID as 入库编号,GoodsID as 货物编号,GoodsName as 货物名称,PrName as 供应商名称,StoreName as 仓库名称,GoodsSpec as 货物规格,GoodsUnit as 计量单位,GoodsNum as 入库数量,GoodsPrice as 进货价格,GoodsAPrice as 总金额,ISDate as 入库日期,HandlePeople as 经手人,ISRe as 备注 from tb_InStore where year(ISDate)=" + P_str_dtime.Substring(0, 4)" and month(ISDate)=" + P_str_dtime.Substring(5, P_str_dtime.Length - 6) + "", "tb_InStore");
DataGridView1.DataSource = myds.Tables[0];
}
上面红色datacon.getds是一个公共类中创建DataSet对象
主要看蓝色代码 我假如输入2009-08 然后查询,未能查询到数据表中记录,高手麻烦看下蓝色代码,给点意见
在FORM窗体中有个combobox1,textBox1,Button和DataGridView。开始选择combobox1.text==入库时间,在textBox1中输入2009-08后可以查询到该条数据库中记录。
主要BUTTON中代码如下:
if (combobox1.Text.Trim() == "入库日期")
{
string P_str_dtime = txtLKWord.Text.Trim();
DataSet myds = datacon.getds("select ISID as 入库编号,GoodsID as 货物编号,GoodsName as 货物名称,PrName as 供应商名称,StoreName as 仓库名称,GoodsSpec as 货物规格,GoodsUnit as 计量单位,GoodsNum as 入库数量,GoodsPrice as 进货价格,GoodsAPrice as 总金额,ISDate as 入库日期,HandlePeople as 经手人,ISRe as 备注 from tb_InStore where year(ISDate)=" + P_str_dtime.Substring(0, 4)" and month(ISDate)=" + P_str_dtime.Substring(5, P_str_dtime.Length - 6) + "", "tb_InStore");
DataGridView1.DataSource = myds.Tables[0];
}
上面红色datacon.getds是一个公共类中创建DataSet对象
主要看蓝色代码 我假如输入2009-08 然后查询,未能查询到数据表中记录,高手麻烦看下蓝色代码,给点意见
解决方案 »
- 如何实现读取word文件,并将每一页以图片的方式发送到客户端?
- 初学者新编的C#计算器代码,另外KeyPress事件想不到该怎么简单的实现,求助...
- ResourceWriter在哪里????在线等.有图有真相
- C#能否作图像处理,比如图形识别?
- 有没有谁用vs2005做过中英文切换呀或者怎么提取资源文件?help me!
- 各位C#大神帮帮忙!!
- 请各位大虾帮忙~ 可否通过类名的字符串来动态创建某各类,或者动态的引用类的某个属性???
- 需要高手回答的问题
- 那位达人能用简单通俗的语言或例子讲一下IEnumerable接口及其应用,写
- Help!Help!Help!操作cdb数据库
- 非常崩溃,C#操作ACCESS全部成功,但数据无变化
- 递归算法
试一下!
或者用时间直接比较 ISDATE<结束时间 and ISDATE>开始时间
DateTime dt;
if(!DateTime.TryParse(txtLKWord.Text, out dt))
{
MessageBox.Show("不是合法日期格式");
}
//...
DataSet myds = datacon.getds("select ISID as 入库编号,GoodsID as 货物编号,GoodsName as 货物名称,PrName as 供应商名称,StoreName as 仓库名称,GoodsSpec as 货物规格,GoodsUnit as 计量单位,GoodsNum as 入库数量,GoodsPrice as 进货价格,GoodsAPrice as 总金额,ISDate as 入库日期,HandlePeople as 经手人,ISRe as 备注 from tb_InStore where year(ISDate)=" + dt.Year.ToString()+" and month(ISDate)=" + dt.Month.ToString() + "", "tb_InStore");
DateTime dt;
if(!DateTime.TryParse(txtLKWord.Text, out dt))
{ MessageBox.Show("不是合法日期格式"); }
//...
DataSet myds = datacon.getds("select ISID as 入库编号,GoodsID as 货物编号,GoodsName as 货物名称,PrName as 供应商名称,StoreName as 仓库名称,GoodsSpec as 货物规格,GoodsUnit as 计量单位,GoodsNum as 入库数量,GoodsPrice as 进货价格,GoodsAPrice as 总金额,ISDate as 入库日期,HandlePeople as 经手人,ISRe as 备注 from tb_InStore where year(ISDate)='" + dt.Year.ToString()+"' and month(ISDate)='" + dt.Month.ToString() + "'", "tb_InStore");
不过我纳闷我这方法为什么查询不到,myds为空
改为
where year(ISDate)=" + P_str_dtime.Substring(0, 4)" and month(ISDate)=" + P_str_dtime.Substring(5, P_str_dtime.Length - 6).TrimStart('0') + ...
{
string P_str_dtime = txtLKWord.Text.Trim();
string dateBegin = Convert.ToDateTime( P_str_dtime+"-01 00:00:00").ToString("yyyy-MM-dd HH:mm:ss");
string dateEnd = Convert.ToDateTime(dateBegin).AddSeconds(-1).ToString("yyyy-MM-dd HH:mm:ss");
DataSet myds = datacon.getds("select ISID as 入库编号,GoodsID as 货物编号,GoodsName as 货物名称,PrName as 供应商名称,StoreName as 仓库名称,GoodsSpec as 货物规格,GoodsUnit as 计量单位,GoodsNum as 入库数量,GoodsPrice as 进货价格,GoodsAPrice as 总金额,ISDate as 入库日期,HandlePeople as 经手人,ISRe as 备注 from tb_InStore where IsDate Between '"+dateBegin+"' and '"+dateEnd+"', "tb_InStore");
DataGridView1.DataSource = myds.Tables[0];
}
既然前台能很容易确定时间范围,干吗让数据库多余做些计算呢?