سمیرا55
شنبه 16 شهریور 1392, 10:06 صبح
با سلام
اگر بخام یک جستجوی چندتایی با دستور like انجام بدم آیا استفاده از or درسته ؟
داخل page ام چندتا textbox دارم و ممکنه کاربر عمل جستجو رو بر اساس textbox1 یا textbox2 یا هر دو انجام بده در این مورد آیا استفاده از or درسته؟ من استفاده که کردم جواب نمیده ولی شرط ها رو جدا جدا بررسی کردم ولی بازم جواب نمیده
if (TextBox1.Text != null && TextBox2.Text == null)
{
string commandtext = "select * from tabl1 where (receiver='"+TextBox1.Text+"')";
SqlCommand cmd = new SqlCommand(commandtext, co);
co.Open();
SqlDataReader dr = cmd.ExecuteReader();
GridView1.DataSource = dr;
GridView1.DataBind();
GridView1.Visible = true;
co.Close();
}
else if (TextBox2.Text != null && TextBox1.Text == null)
{
string commandtext = "select * from tabl1 where (massage_text like N'%" + TextBox2.Text + "%')";
SqlCommand cmd = new SqlCommand(commandtext, co);
co.Open();
SqlDataReader dr = cmd.ExecuteReader();
GridView1.DataSource = dr;
GridView1.DataBind();
GridView1.Visible = true;
co.Close();
}
else if (TextBox1.Text != null && TextBox2.Text != null)
{
string commandtext = "select * from tabl1 where (receiver like N'%" + TextBox1.Text + "%') OR (massage_text like N'%" + TextBox2.Text + "%')";
SqlCommand cmd = new SqlCommand(commandtext, co);
co.Open();
SqlDataReader dr = cmd.ExecuteReader();
GridView1.DataSource = dr;
GridView1.DataBind();
GridView1.Visible = true;
co.Close();
}
}
اگر بخام یک جستجوی چندتایی با دستور like انجام بدم آیا استفاده از or درسته ؟
داخل page ام چندتا textbox دارم و ممکنه کاربر عمل جستجو رو بر اساس textbox1 یا textbox2 یا هر دو انجام بده در این مورد آیا استفاده از or درسته؟ من استفاده که کردم جواب نمیده ولی شرط ها رو جدا جدا بررسی کردم ولی بازم جواب نمیده
if (TextBox1.Text != null && TextBox2.Text == null)
{
string commandtext = "select * from tabl1 where (receiver='"+TextBox1.Text+"')";
SqlCommand cmd = new SqlCommand(commandtext, co);
co.Open();
SqlDataReader dr = cmd.ExecuteReader();
GridView1.DataSource = dr;
GridView1.DataBind();
GridView1.Visible = true;
co.Close();
}
else if (TextBox2.Text != null && TextBox1.Text == null)
{
string commandtext = "select * from tabl1 where (massage_text like N'%" + TextBox2.Text + "%')";
SqlCommand cmd = new SqlCommand(commandtext, co);
co.Open();
SqlDataReader dr = cmd.ExecuteReader();
GridView1.DataSource = dr;
GridView1.DataBind();
GridView1.Visible = true;
co.Close();
}
else if (TextBox1.Text != null && TextBox2.Text != null)
{
string commandtext = "select * from tabl1 where (receiver like N'%" + TextBox1.Text + "%') OR (massage_text like N'%" + TextBox2.Text + "%')";
SqlCommand cmd = new SqlCommand(commandtext, co);
co.Open();
SqlDataReader dr = cmd.ExecuteReader();
GridView1.DataSource = dr;
GridView1.DataBind();
GridView1.Visible = true;
co.Close();
}
}