把它写成短篇小说
我想要类似“SELECT companyName FROM table where mainCategory = firstcombobox and subcategory = secondcombobox”这样的东西,我该如何进行 sql 查询?
========================== 说来话长
我已经创建了一个带有有效编码的表单,但我需要额外的帮助。
有点,我一直在试图弄清楚如何让第 3 个组合框的值由第一个和第二个决定。
我想要的是,获取 Main Category 和 Sub category 的值来影响第三个组合框的列表。
我只需要 SQL 查询,例如:“SELECT companyName FROM table where maincategory = firstcombobox and subcategory = secondcombobox”
然后在主分类和子分类的选择中显示公司名称。
像这样:
对于 Main Category 和 Sub-Category ,我让它使用这段代码。 此代码还包括第三个组合框代码,该代码现在在没有附加到第三个组合框代码的第一个和第二个组合框的情况下运行。
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();
}
- 同样,现在第三个组合框被编码为在没有主类别和子类别的情况下运行
========================
已回答 - 创建了一个名为搜索的按钮,并将表单加载中的代码放入按钮中,添加了 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;
}
最佳答案
尝试在 mainCatU
和 subCatU
组合的 SelectedValue
更改事件上调用以下函数:
private void SetCompanyList()
{
if (string.IsNullOrEmpty(Convert.ToString(mainCatU.SelectedValue)) || string.IsNullOrEmpty(Convert.ToString(subCatU.SelectedValue))) return;
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=\\SQLEXPRESS;Initial Catalog=master;Integrated Security=True";
conn.Open();
SqlDataAdapter daMain = new SqlDataAdapter("SELECT cName FROM ComDet where mainCate = @mainCat subCat = @subCate", conn);
daMain.SelectCommand.Parameters.Add("@mainCat", SqlDbType.VarChar).Value = mainCatU.SelectedValue;
daMain.SelectCommand.Parameters.Add("@subCate", SqlDbType.VarChar).Value = subCatU.SelectedValue;
DataTable _table = new DataTable();
daMain.Fill(_table);
ListU.DataSource = _table;
}
关于c# - 来自 2 ComboBox 的类别影响带有 SQL 的 datagridview 的第三个 Combobox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20779276/