【问题标题】:C# - Pull SQL data to textboxes/labels from changing combobox selected itemC# - 从更改组合框选定项中将 SQL 数据拉到文本框/标签
【发布时间】:2019-10-04 17:26:18
【问题描述】:

所以我正在尝试创建一种网站概览实用程序,每个网站都有不同的信息。

我想要一个下拉列表/组合框,读取 sqldb 并根据 db 创建项目。然后我想用不同的文本框填充列中的值。

假设我的数据库表到目前为止被称为“AvSites”(只是为了它)我有“projectNr”、“siteName”、“siteClients”和“siteLicenses”列我希望这些列中的每一个都填充一些文本框/标签某处。

我尝试了以下方法,这有点用,我的代码大部分时间都在工作,但让我失望的是选择组合框项后数据会发生变化。

我希望你能帮忙,所以这是我目前的代码(我有一个登录窗口,在这个“主”程序启动之前,所以你不会想知道)

而且我对 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();

【问题讨论】:

    标签: c# sql winforms combobox textbox


    【解决方案1】:

    您无需再次调用数据库。所有信息都在当前选定的项目中。
    当您像在单击事件中一样将组合的 DataSource 设置为数据表时,组合的每个元素都是 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 类将其读回。了解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
    

    【讨论】:

    • 谢谢。我实际上想知道两次调用 db,我记得认为必须有一个更简单的方法。至于改进,我当然会实施它们,正如我所说,我已经在认真的决心上涉足了大约一个月的时间。再次感谢您的意见,您为我省去了很多麻烦
    猜你喜欢
    • 2018-01-29
    • 1970-01-01
    • 1970-01-01
    • 2015-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-06
    相关资源
    最近更新 更多