比如数据库中有一张表里面  (入库时间)字段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 然后查询,未能查询到数据表中记录,高手麻烦看下蓝色代码,给点意见

解决方案 »

  1.   

    where left (convert(char(10),ISDate,120),7)='" + P_str_dtime+"'" 
    试一下!
      

  2.   

    把你的查询字符串用消息对话框弹出看看是什么。这是很不可取的一种方式。如果用户输入的是2008-04,你需要把这种字符串转换为时间类型。再与数据库里比较,最后用between 2008-04-01 and 2008-05-01之间的数据。
    或者用时间直接比较 ISDATE<结束时间 and ISDATE>开始时间
      

  3.   

    where year(ISDate)='" + P_str_dtime.Substring(0, 4)"' and month(ISDate)='" + P_str_dtime.Substring(5, P_str_dtime.Length - 6) + "' 
      

  4.   


    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"); 
      

  5.   


    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");
      

  6.   

    后面有个+,是我漏写的
    不过我纳闷我这方法为什么查询不到,myds为空
      

  7.   

    设置断点跟踪SQL语句吧!P_str_dtime.Length - 6--------把6改成5试试。
      

  8.   

     month(ISDate) 是不会自动补 0 滴!!!where year(ISDate)=" + P_str_dtime.Substring(0, 4)" and month(ISDate)=" + P_str_dtime.Substring(5, P_str_dtime.Length - 6)
    改为
    where year(ISDate)=" + P_str_dtime.Substring(0, 4)" and month(ISDate)=" + P_str_dtime.Substring(5, P_str_dtime.Length - 6).TrimStart('0') + ...
      

  9.   

    where year(ISDate)=" + P_str_dtime.Substring(0, 4)" and month(ISDate)=" + P_str_dtime.Substring(5, P_str_dtime.Length - 6).TrimStart('0') + ...再改为where year(ISDate)=" + P_str_dtime.Substring(0, 4)" and month(ISDate)=" + P_str_dtime.Substring(5).TrimStart('0') + ...
      

  10.   

    if (combobox1.Text.Trim() == "入库日期") 
      { 
        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]; 
      } 
    既然前台能很容易确定时间范围,干吗让数据库多余做些计算呢?
      

  11.   

    对数据库而言。。如果这个表数据量大,而且ISdate字段上建立了索引,那么这种写法Year(IsDate)、Month(IsDate)将不会用到索引。。