سلام
namespace search
{
public partial class Form1 : Form
{
string searchQuery;
SqlConnection sc = new SqlConnection("Data Source=.;Initial Catalog=......;Integrated Security=True");
SqlCommand cmd = new SqlCommand();
DataSet ds = new DataSet();
DataTable dt = new DataTable();
BindingSource bind = new BindingSource();
public Form1()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
sc.Open();
string strSql =" Select B.IdBook as [کدکتاب],B.TitleBook as [عنوان کتاب],";
strSql +=" B.Publisher as[ناشر],B.CountBook as[تعداد],";
strSql +=" B.ShabakCode as[کدشابک],B.Language as[زبان],";
strSql +=" B.Year as[سال چاپ],S.Subject as[موضوع],";
strSql +=" W.Writer as[نویسنده],T.Translator as[مترجم] ";
strSql +=" from tblBook B left outer join tblSubject S on B.IdSubject=S.IdSubject ";
strSql +=" INNER join tblWriter W on B.IdWriter=W.IdWriter ";
strSql +=" INNER join tblTrans T on B.IdTranslator=T.IdTranslator ";
string strWhere = "";
if (comboBox1.SelectedIndex == 0)
{
if (comboBox2.SelectedIndex == 0)
strWhere = " Where B.IdBook= " + textBox1.Text.Trim();
if (comboBox2.SelectedIndex == 1)
strWhere = " where B.TitleBook LIKE N'" + textBox1.Text.Trim() + "%'";
if (comboBox2.SelectedIndex == 2)
strWhere = " where (S.Subject LIKE N'" + textBox1.Text.Trim() + "%')";
if (comboBox2.SelectedIndex == 3)
strWhere = " where (B.Publisher LIKE N'" + textBox1.Text.Trim() + "%')";
if (comboBox2.SelectedIndex == 4)
strWhere = " where (W.Writer LIKE N'" + textBox1.Text.Trim() + "%')";
}
else
{
if (comboBox1.SelectedIndex == 1)
{
if (comboBox2.SelectedIndex == 0)
strWhere = " where (W.Writer LIKE N'" + textBox1.Text.Trim() + "%')";
if (comboBox2.SelectedIndex == 1)
strWhere = " where (T.Translator LIKE N'" + textBox1.Text.Trim() + "%')";
if (comboBox2.SelectedIndex == 2)
strWhere = " where (S.Subject LIKE N'" + textBox1.Text.Trim() + "%')";
}
}
searchQuery = strSql + strWhere;
dt.Clear();
SqlDataAdapter da = new SqlDataAdapter(searchQuery, sc);
da.Fill(dt);
dataGridViewsearch.DataSource = dt;
sc.Close();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
comboBox2.Items.Clear();
comboBox2.Text = "";
if (comboBox1.SelectedIndex == 0)
{
comboBox2.Items.Add("کدکتاب");
comboBox2.Items.Add("نام کتاب");
comboBox2.Items.Add("موضوع");
comboBox2.Items.Add("ناشر");
comboBox2.Items.Add("نویسنده");
}
else
if (comboBox1.SelectedIndex == 1)
{
comboBox2.Items.Add("نویسنده");
comboBox2.Items.Add("مترجم");
comboBox2.Items.Add("موضوع");
}
}
}
}
بقیه را هم با سلیقه خودتون تکمیل کنید