我数据库中有个一表,其中有个时间的字段,它的格式是年-月-日 我想对其进行查询,且只查询它的 年月,日不要。查询方式是:根据我输入的年月查询,把结果显示在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());
}
我数据库表的建立如下: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());
}
肯定不行的饿
应该是这个意思吧
where month(time)='" + textBox1.Text + "' and year(time)='" + textBox2.Text + "'
....
这样的SQL能写吗?? 前面是表里面的列..
where month(time)='" + textBox1.Text + "' and year(time)='" + textBox2.Text + "'