我创建了一个datagridview和3个复选框名称:chkfilename,chkfiledate,chkexdate。
我使用每个复选框的条件搜索。例如:
if (chkFilename.Checked)
{
if (con.State == ConnectionState.Open)
{
con.Close();
}
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from TBLBackupFile where DestinationFileName like '%" + txtFileName.Text + "%' or SourceFileName like '%" + txtFileName.Text + "%'";
//cmd.CommandText = "Select * from TBLBackupFile";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
dataGridView1.DataSource = dt;
- 条件搜索chkfiledate:
if (chkFileDate.Checked)
{
DateTime fromdate = DateTime.Parse(dateTimePicker1.Text);
DateTime todate = DateTime.Parse(dateTimePicker2.Text);
if (con.State == ConnectionState.Open)
{
con.Close();
}
con.Open();
SqlCommand cmd1 = con.CreateCommand();
cmd1.CommandType = CommandType.Text;
cmd1.CommandText = "select * from TBLBackupFile where SourceFileDate>='" + fromdate.ToString("yyyy/MM/dd 00:00:00") + "' and SourceFileDate <= '" + todate.ToString("yyyy/MM/dd 23:59:59") + "'";
cmd1.ExecuteNonQuery();
DataTable dt1 = new DataTable();
SqlDataAdapter da1 = new SqlDataAdapter(cmd1);
da1.Fill(dt1);
dataGridView1.DataSource = dt1;
}
- 如果我设置:
,我如何使用条件搜索2复选框if (chkFilename.Checked && chkFileDate.Checked)
{
}
答案 0 :(得分:2)
如果我正确理解您的问题,您希望根据复选框检查状态应用条件。以下是可能对您有用的方法之一
string whrCond = "";
if (chkFilename.Checked)
{
whrCond = "DestinationFileName like '%" + txtFileName.Text + "%' or SourceFileName like '%" + txtFileName.Text + "%'";
}
else if (chkFileDate.Checked)
{
if(chkFilename.Checked)
whrCond = whrCond + " AND "
whrCond = whrCond + "SourceFileDate>='" + fromdate.ToString("yyyy/MM/dd 00:00:00") + "' and SourceFileDate <= '" + todate.ToString("yyyy/MM/dd 23:59:59") + "'";
}
else if (chkexdate.Checked)
{
if(chkFileDate.Checked || chkFilename.Checked)
whrCond = whrCond + " AND "
whrCond = whrCond + //Whatever your condition is
}
else
{
whrCond = "1 = 1";
}
if (con.State == ConnectionState.Open)
{
con.Close();
}
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from TBLBackupFile where " + whrCond;
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
dataGridView1.DataSource = dt;
注意: 我没有运行程序,所以把它当作算法而不是解决方案
答案 1 :(得分:1)
您可以使用以下代码:
var conditions = new List<string>();
if(chkFilename.Checked)
{
conditions.Add(string.Format("DestinationFileName LIKE '%{0}%' OR SourceFileName LIKE '%{0}%'", txtFileName.Text));
}
if(chkFileDate.Checked)
{
DateTime fromdate = DateTime.Parse(dateTimePicker1.Text);
DateTime todate = DateTime.Parse(dateTimePicker2.Text);
conditions.Add(string.Format("SourceFileDate>='{0:yyyy/MM/dd 00:00:00}' AND SourceFileDate <= '{1:yyyy/MM/dd 23:59:59}'", fromdate, todate));
}
if (con.State == ConnectionState.Open)
con.Close();
con.Open();
using(SqlCommand cmd1 = con.CreateCommand())
{
cmd1.CommandType = CommandType.Text;
var condition = "";
if(conditions.Any())
condition = string.Format("WHERE {0}", string.Join(" AND ", conditions.Select(c => "(" + c + ")")));
cmd1.CommandText = string.Format("SELECT * FROM TBLBackupFile {0}", condition);
cmd1.ExecuteNonQuery();
DataTable dt1 = new DataTable();
SqlDataAdapter da1 = new SqlDataAdapter(cmd1);
da1.Fill(dt1);
dataGridView1.DataSource = dt1;
}