PDA

View Full Version : جستجو چندتایی با استفاده از دستور like



سمیرا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();
}
}

LostOfMind
شنبه 16 شهریور 1392, 11:14 صبح
سلام
ببینید این کد نمونه پاسخگو است؟

SqlDataAdapter da;
DataTable dt;
if (TextBox1.Text != string.Empty && TextBox2.Text != string.Empty)
{
da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand();
da.SelectCommand.Connection = _ConnectionString._Conn;
da.SelectCommand.CommandText = @"SELECT announcementsID, PostCategoryID, CreateDate, LastEditDate, UserName, Title, Abstract, Body, IsActive FROM Announcements where ([UserName] like N'%" + TextBox1.Text + "%')" + " Or (Title like N'%" + TextBox2.Text + "%')";
dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
else if (TextBox1.Text != string.Empty)
{
da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand();
da.SelectCommand.Connection = _ConnectionString._Conn;
da.SelectCommand.CommandText = @"SELECT announcementsID, PostCategoryID, CreateDate, LastEditDate, UserName, Title, Abstract, Body, IsActive FROM Announcements where ([UserName] like N'%" + TextBox1.Text + "%')";
dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
else if (TextBox2.Text != string.Empty)
{
da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand();
da.SelectCommand.Connection = _ConnectionString._Conn;
da.SelectCommand.CommandText = @"SELECT announcementsID, PostCategoryID, CreateDate, LastEditDate, UserName, Title, Abstract, Body, IsActive FROM Announcements where ([Title] like N'%" + TextBox2.Text + "%')";
dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}

سمیرا55
شنبه 16 شهریور 1392, 12:14 عصر
واقعا ممنونم . خیلی لطف کردید