【发布时间】:2014-01-13 18:45:15
【问题描述】:
把它写成短篇小说
我想要类似 "SELECT companyName FROM table where mainCategory = firstcombobox and subcategory = secondcombobox" 之类的东西,我该如何进行 sql 查询?
=========================== 长篇大论
我已经创建了一个表格,并带有有效的编码,但我需要额外的帮助。
有点,我一直在试图弄清楚如何让第三个组合框值由第一个和第二个决定。
我想要的是,获取主类别和子类别的值以影响第三个组合框的列表。
我只需要 SQL 查询,例如:“SELECT companyName FROM table where maincategory = firstcombobox and subcategory = secondcombobox”
然后在主要和子类别的选择中显示公司名称。
像这样:
对于 Main Category 和 Sub-Category ,我让它使用此代码。 此代码还包括第 3 个 ComboBox 代码,该代码现在可以在第一个和第二个组合框未附加到第 3 个组合框代码的情况下运行。
public partial class User : Form
{
Dictionary<string, List<string>> Category = new Dictionary<string, List<string>>();
DataSet ds1;
public User()
{
InitializeComponent();
}
private void User_Load(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=\\SQLEXPRESS;Initial Catalog=master;Integrated Security=True";
conn.Open();
SqlDataAdapter daMain = new SqlDataAdapter("SELECT * FROM MAINCATE", conn);
ds1 = new DataSet();
daMain.Fill(ds1, "Maincate");
DataTable dt = ds1.Tables["MAINCATE"];
foreach (DataRow dr in dt.Rows)
{
List<string> SubCats = new List<string>
{
dr["Subcat1"].ToString(),
dr["Subcat2"].ToString(),
dr["Subcat3"].ToString(),
dr["Subcat4"].ToString()
};
Category.Add(dr["mainCate"].ToString(), SubCats);
mainCatU.Items.Add(dr["mainCate"].ToString());
}
mainCatU.DropDownStyle = ComboBoxStyle.DropDownList;
mainCatU.Enabled = true;
subCatU.DropDownStyle = ComboBoxStyle.DropDownList;
//**Code for third combobox**
SqlDataAdapter daSearch = new SqlDataAdapter("SELECT cName FROM ComDet", conn);
DataTable dt1 = new DataTable();
ListU.DataSource = dt1;
daSearch.Fill(dt1);
ListU.ValueMember = "cName";
ListU.DisplayMember = "cName";
ListU.DropDownStyle = ComboBoxStyle.DropDownList;
ListU.Enabled = true;
//**----------------------**
conn.Close();
}
private void mainCatU_SelectedIndexChanged(object sender, EventArgs e)
{
if(Category.ContainsKey(mainCatU.SelectedItem.ToString()))
{
subCatU.DataSource = Category[mainCatU.SelectedItem.ToString()];
}
}
数据库如图所示:
dbo.MAINCATE
dbo.ComDet
View Selected Company button 的代码是:
private void searchBtn_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=PEWPEWDIEPIE\\SQLEXPRESS;Initial Catalog=master;Integrated Security=True";
conn.Open();
SqlDataAdapter daS = new SqlDataAdapter("select cName, cDetails, cDetails2 from ComDet where cName = @cName", conn);
daS.SelectCommand.Parameters.Add("@cName", SqlDbType.VarChar).Value = ListU.SelectedValue;
DataTable dts3 = new DataTable();
daS.Fill(dts3);
dataGridView1.DataSource = dts3.DefaultView;
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
conn.Close();
}
- 同样,现在第三个组合框被编码为在没有主类别和子类别的情况下运行
==========================
已回答 - 创建了一个名为 search 的按钮,并将表单加载中的编码放入按钮中,添加了 SQLCon ,并添加了所需的 SQLQuery ..
谢谢各位.. :)
private void button2_Click_1(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=PEWPEWDIEPIE\\SQLEXPRESS;Initial Catalog=master;Integrated Security=True";
conn.Open();
string myRequest = "SELECT cName FROM ComDet where mainCate = '" + mainCatU.SelectedItem.ToString() + "' and Subcat = '" + subCatU.SelectedItem.ToString() + "'";
SqlDataAdapter daSearch = new SqlDataAdapter(myRequest, conn);
DataTable dtSea = new DataTable();
ListU.DataSource = dtSea;
daSearch.Fill(dtSea);
ListU.ValueMember = "cName";
ListU.DisplayMember = "cName";
ListU.DropDownStyle = ComboBoxStyle.DropDownList;
ListU.Enabled = true;
}
【问题讨论】:
-
真的有人能帮忙吗?
标签: c# sql search datagridview combobox