Winforms 组合框和参数化存储过程
Winforms comboboxes and parameterized stored procedure
在我的 tblSates Table 中,我有一个名为 Monat 的列,其中包含这三个值
[01.2016、02.2016 和 03.2016] 我想在组合框中获取这些值。
我正在获取值,但只有其中两个而不是全部三个。
这是我的代码:
private void FillCombobox2()
{
string S = ConfigurationManager
// TSQL-Statement
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = ("SELECT DISTINCT Monat from tblSales");
SqlDataReader myReader;
try
{
con.Open();
myReader = cmd.ExecuteReader();
if (myReader.Read())
{
DataTable dt = new DataTable();
dt.Load(myReader);
combobox1.DisplayMember = "Monat";
combobox1.ValueMember = "Monat";
combobox1.DataSource = dt;
combobox1.SelectedIndex = -1;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
con.Close();
}
}
将不胜感激任何帮助或替代解决方案。
我删除了 reader.Read,然后调用它提升了位置(并跳过了我的三个记录之一)。或者我可以使用 if (myReader.HasRows)
.
private void FillCombobox2()
{
string S = ConfigurationManager
// TSQL-Statement
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = ("SELECT DISTINCT Monat from tblSales");
//SqlDataReader myReader;
try
{
con.Open();
SqlDataAdapter ad = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
ad.Fill(dt)
combobox1.DisplayMember = "Monat";
combobox1.ValueMember = "Monat";
combobox1.DataSource = dt;
combobox1.SelectedIndex = -1;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
con.Close();
}
}
在我的 tblSates Table 中,我有一个名为 Monat 的列,其中包含这三个值 [01.2016、02.2016 和 03.2016] 我想在组合框中获取这些值。
我正在获取值,但只有其中两个而不是全部三个。
这是我的代码:
private void FillCombobox2()
{
string S = ConfigurationManager
// TSQL-Statement
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = ("SELECT DISTINCT Monat from tblSales");
SqlDataReader myReader;
try
{
con.Open();
myReader = cmd.ExecuteReader();
if (myReader.Read())
{
DataTable dt = new DataTable();
dt.Load(myReader);
combobox1.DisplayMember = "Monat";
combobox1.ValueMember = "Monat";
combobox1.DataSource = dt;
combobox1.SelectedIndex = -1;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
con.Close();
}
}
将不胜感激任何帮助或替代解决方案。
我删除了 reader.Read,然后调用它提升了位置(并跳过了我的三个记录之一)。或者我可以使用 if (myReader.HasRows)
.
private void FillCombobox2()
{
string S = ConfigurationManager
// TSQL-Statement
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = ("SELECT DISTINCT Monat from tblSales");
//SqlDataReader myReader;
try
{
con.Open();
SqlDataAdapter ad = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
ad.Fill(dt)
combobox1.DisplayMember = "Monat";
combobox1.ValueMember = "Monat";
combobox1.DataSource = dt;
combobox1.SelectedIndex = -1;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
con.Close();
}
}