我数据库中有个一表,其中有个时间的字段,它的格式是年-月-日 我想对其进行查询,且只查询它的 年月,日不要。查询方式是:根据我输入的年月查询,把结果显示在datagridview中。我的SELECT语句是:select month(time)='" + textBox1.Text + "' and year(time)='" + textBox2.Text + "' from timetest 
我数据库表的建立如下:create table timetest
(
id int identity(1,1) primary key,
name varchar(20),
time datetime 
)
在我点击查询后,出错提示:=号附近有错误。我把代码贴出来: 
string sql = "select month(time)='" + textBox1.Text + "' and year(time)='" + textBox2.Text + "' from timetest "; 
SqlConnection con = new SqlConnection("server=.;database=cconwinform;uid=sa;pwd=263490"); 
SqlDataAdapter sda = new SqlDataAdapter(); 
try 

sda.SelectCommand = new SqlCommand(sql, con); 
DataTable dt = new DataTable(); 
sda.Fill(dt); 
this.dataGridView1.DataSource = dt; 

catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

解决方案 »

  1.   

    year(time)返回的是整形
    肯定不行的饿
      

  2.   

    select * from timetest where month(time)='" + textBox1.Text + "' and year(time)='" + textBox2.Text + "'
    应该是这个意思吧
      

  3.   

    select cast(month(time) as varchar)='" + textBox1.Text + "' and cast(year(time) as varchar)='" + textBox2.Text + "' from timetest 
      

  4.   

    select * from timetest where month(time)=....
      

  5.   

    select month(time),year(time) from timetest
    where month(time)='" + textBox1.Text + "' and year(time)='" + textBox2.Text + "' 
      

  6.   

    select month(time)='一个字符串' from timetest 
    ....

    这样的SQL能写吗?? 前面是表里面的列..
      

  7.   

    select * from timetest
    where month(time)='" + textBox1.Text + "' and year(time)='" + textBox2.Text + "' 
      

  8.   

    这样。select * from timetest where month(time)='" + textBox1.Text + "' and year(time)='" + textBox2.Text + "'