C# - 从更改组合框选定项中将 SQL 数据拉取到 textboxes/labels
C# - Pull SQL data to textboxes/labels from changing combobox selected item
所以我正在尝试创建一种网站概览实用程序,每个网站上都有不同的信息。
我想要一个下拉列表/组合框,读取一个sqldb并根据数据库创建项目。然后我想要不同的文本框来填充列中的值。
假设我的数据库 table 到目前为止被称为 "AvSites"(只是为了它)我有 "projectNr"、"siteName"、"siteClients" 和"siteLicenses" 列我希望每一列都在某处填充一些文本框/标签。
我尝试了以下方法,这有点管用,我的代码大部分时间都在工作,但让我失望的是数据随着选择的组合框项目而改变。
我希望你能帮忙,所以这是我到目前为止的代码(我有一个登录 window,在这个 "main" 程序启动之前,只是为了让你不会想知道)
而且我对 C# 还很陌生,所以如果有什么地方做的效率低下,那就是 :) 我还在学习。
namespace AvOverview{
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
}
private void Button1_Click(object sender, EventArgs e)
{
//btn_LogOut Click Event
this.Hide();
Form1 fl = new Form1();
fl.Show();
}
private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
{
Application.Exit();
}
private void FrmMain_Load(object sender, EventArgs e)
{
string cs = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Database1.mdf";
SqlConnection con = new SqlConnection(cs);
con.Open();
string strCmd = "select * from AvSites";
SqlCommand cmd = new SqlCommand(strCmd, con);
SqlDataAdapter da = new SqlDataAdapter(strCmd, con);
DataSet ds = new DataSet();
da.Fill(ds);
combo1.ValueMember = "id";
combo1.DisplayMember = "siteName";
combo1.DataSource = ds.Tables[0];
combo1.Enabled = true;
this.combo1.SelectedIndex = -1;
cmd.ExecuteNonQuery();
con.Close();
}
private void Combo1_SelectedIndexChanged(object sender, EventArgs e)
{
string cs = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Database1.mdf";
string strCmd = "select id from AvSites";
SqlConnection con = new SqlConnection(cs);
SqlCommand cmd = new SqlCommand(strCmd, con);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{//this last part is solely for testing if the text changed the way I wanted.
label1.Text = dr.GetValue(1).ToString();
label2.Text = dr.GetValue(2).ToString();
label3.Text = dr.GetValue(0).ToString();
label4.Text = dr.GetValue(3).ToString();
您不需要再次调用数据库。所有信息都在当前选中的项目中。
当您将组合的数据源设置为数据表时,就像您在点击事件中所做的那样,组合的每个元素都是一个 DataRowView,并且您可以从该元素中获取从初始查询中从数据库中提取的所有信息
private void Combo1_SelectedIndexChanged(object sender, EventArgs e)
{
DataRowView rv = Combo1.SelectedItem as DataRowView;
if(rv != null)
{
label1.Text = rv[1].ToString();
label2.Text = rv[2].ToString();
label3.Text = rv[0].ToString();
label4.Text = rv[3].ToString();
}
}
旁注:您的代码需要进行一些改进。
首先,您应该将连接字符串存储在配置文件中,然后使用 ConfigurationManager class 读取它。了解 Configuration in NET
其次,您不应该像现在这样使用一次性物品。一次性物品应在您用完后立即丢弃。特别是 SqlConnection 在您的机器和服务器上都保留了宝贵的系统资源。您应该开始使用 using statement
string strCmd = "select * from AvSites";
using(SqlConnection con = new SqlConnection(.......))
using(SqlCommand cmd = new SqlCommand(strCmd, con)))
{
con.Open();
SqlDataAdapter da = new SqlDataAdapter(strCmd, con);
DataSet ds = new DataSet();
da.Fill(ds);
combo1.ValueMember = "id";
combo1.DisplayMember = "siteName";
combo1.DataSource = ds.Tables[0];
combo1.Enabled = true;
this.combo1.SelectedIndex = -1;
// ??? not needed ==> cmd.ExecuteNonQuery();
// not needed with using ==> con.Close();
}
// Here the connection is closed and disposed and resources are released
所以我正在尝试创建一种网站概览实用程序,每个网站上都有不同的信息。
我想要一个下拉列表/组合框,读取一个sqldb并根据数据库创建项目。然后我想要不同的文本框来填充列中的值。
假设我的数据库 table 到目前为止被称为 "AvSites"(只是为了它)我有 "projectNr"、"siteName"、"siteClients" 和"siteLicenses" 列我希望每一列都在某处填充一些文本框/标签。
我尝试了以下方法,这有点管用,我的代码大部分时间都在工作,但让我失望的是数据随着选择的组合框项目而改变。
我希望你能帮忙,所以这是我到目前为止的代码(我有一个登录 window,在这个 "main" 程序启动之前,只是为了让你不会想知道)
而且我对 C# 还很陌生,所以如果有什么地方做的效率低下,那就是 :) 我还在学习。
namespace AvOverview{
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
}
private void Button1_Click(object sender, EventArgs e)
{
//btn_LogOut Click Event
this.Hide();
Form1 fl = new Form1();
fl.Show();
}
private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
{
Application.Exit();
}
private void FrmMain_Load(object sender, EventArgs e)
{
string cs = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Database1.mdf";
SqlConnection con = new SqlConnection(cs);
con.Open();
string strCmd = "select * from AvSites";
SqlCommand cmd = new SqlCommand(strCmd, con);
SqlDataAdapter da = new SqlDataAdapter(strCmd, con);
DataSet ds = new DataSet();
da.Fill(ds);
combo1.ValueMember = "id";
combo1.DisplayMember = "siteName";
combo1.DataSource = ds.Tables[0];
combo1.Enabled = true;
this.combo1.SelectedIndex = -1;
cmd.ExecuteNonQuery();
con.Close();
}
private void Combo1_SelectedIndexChanged(object sender, EventArgs e)
{
string cs = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Database1.mdf";
string strCmd = "select id from AvSites";
SqlConnection con = new SqlConnection(cs);
SqlCommand cmd = new SqlCommand(strCmd, con);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{//this last part is solely for testing if the text changed the way I wanted.
label1.Text = dr.GetValue(1).ToString();
label2.Text = dr.GetValue(2).ToString();
label3.Text = dr.GetValue(0).ToString();
label4.Text = dr.GetValue(3).ToString();
您不需要再次调用数据库。所有信息都在当前选中的项目中。
当您将组合的数据源设置为数据表时,就像您在点击事件中所做的那样,组合的每个元素都是一个 DataRowView,并且您可以从该元素中获取从初始查询中从数据库中提取的所有信息
private void Combo1_SelectedIndexChanged(object sender, EventArgs e)
{
DataRowView rv = Combo1.SelectedItem as DataRowView;
if(rv != null)
{
label1.Text = rv[1].ToString();
label2.Text = rv[2].ToString();
label3.Text = rv[0].ToString();
label4.Text = rv[3].ToString();
}
}
旁注:您的代码需要进行一些改进。
首先,您应该将连接字符串存储在配置文件中,然后使用 ConfigurationManager class 读取它。了解 Configuration in NET
其次,您不应该像现在这样使用一次性物品。一次性物品应在您用完后立即丢弃。特别是 SqlConnection 在您的机器和服务器上都保留了宝贵的系统资源。您应该开始使用 using statement
string strCmd = "select * from AvSites";
using(SqlConnection con = new SqlConnection(.......))
using(SqlCommand cmd = new SqlCommand(strCmd, con)))
{
con.Open();
SqlDataAdapter da = new SqlDataAdapter(strCmd, con);
DataSet ds = new DataSet();
da.Fill(ds);
combo1.ValueMember = "id";
combo1.DisplayMember = "siteName";
combo1.DataSource = ds.Tables[0];
combo1.Enabled = true;
this.combo1.SelectedIndex = -1;
// ??? not needed ==> cmd.ExecuteNonQuery();
// not needed with using ==> con.Close();
}
// Here the connection is closed and disposed and resources are released