表的字段为:
id:varchar
sj: datetime目的是为了查询两个时间段内的记录:
dtpbg :datatime
dtped :       
代码如下:
private void btnfind1_Click(object sender, EventArgs e)
        {
    
            SqlConnection conn = new SqlConnection("server =61.150.91.149;database = baidatong;uid =baidatong_f;pwd =cbG62lQIlZ ");
            string str1 = "select * from  h_xiaofeijilu where sj between'"+dtpbg+"' and '"+dtped +"'";
        
            DataSet ds1 = new DataSet();
            SqlDataAdapter da1 = new SqlDataAdapter(str1 ,conn);
          
            da1.Fill(ds1, "h_xiaofeijilu");
 
            dgv1.DataSource = ds1 .Tables ["h_xiaofeijilu"].DefaultView;红字的部分给出了一下提示:
   从字符串转换为datetime时发生语法错误。

解决方案 »

  1.   

    dtpbg :datatime 
    dtped :datetime
    string str1 = "select * from  h_xiaofeijilu where sj between'"+dtpbg.tostring("yyyy-MM-dd")+"' and '"+dtped.tostring("yyyy-MM-dd")+"'"; 
      

  2.   

    按楼上的方法
    又有新的提示说:
    “ToString”方法没有采用“1”个参数的重载
    是什么意思呢?
    还有小弟刚入门,不知道为什么要转换成string呢?]“ToString”方法的意思是什么呢?
    望不吝赐教
      

  3.   

    between'dtpbg+' and '+dtped +'"; 
    把双引号去掉试一下
      

  4.   

    string str1 = "select * from  h_xiaofeijilu where sj between'"+string.format(dtpbg,"yyyy-MM-dd"))+"' and '"+string.format(dtped,"yyyy-MM-dd")+"'"; 
      

  5.   

    string str1 = "select * from  h_xiaofeijilu where sj between'"+string.format(dtpbg,"yyyy-MM-dd"))+"' and '"+string.format(dtped,"yyyy-MM-dd")+"'"; 
    这就是对了,结贴吧
      

  6.   

    string str1 = "select * from  h_xiaofeijilu where sj between'"+string.format(dtpbg,"yyyy-MM-dd"))+"' and '"+string.format(dtped,"yyyy-MM-dd")+"'"; 
    出现以下提示:错误 1 “string”并不包含“format”的定义
      

  7.   


    string str1 = "select * from  h_xiaofeijilu where sj between'" + dtpbg.ToString("yyyy-MM-dd") + "' and '" + dtped.ToString("yyyy-MM-dd") + "'";

    string str1 = "select * from  h_xiaofeijilu where sj between'" + dtpbg.ToShortDateString() + "' and '" + dtped.ToShortDateString() + "'";ToString()就是将你的日期转换成字符串,因为你在拼接字符串,日期类型的不能拼接,必须转换成String才行。
    ToString("yyyy-MM-dd")就是按照"yyyy-MM-dd"的格式转换成String。
      

  8.   

    String.Format()是将指定的 String 中的每个格式项替换为相应对象的值的文本等效项。而不是将指定的DateTime中的每个格式项替换为相应对象的值的文本等效项。
      

  9.   

    format=Format如果都还是不行,那你看看你的变量,本身能不能转换为日期哦!
      

  10.   

    String的Format方法参数列表里没有提供DateTime类型的,你别误导别人了。