【问题标题】:populate form values based on data table in C#在 C# 中根据数据表填充表单值
【发布时间】:2014-02-15 19:19:43
【问题描述】:

我返回一行的数据表,然后设置数据表的主键,然后查找该列。然后我想将从company 列返回的值分配给txtCompany

这是我尝试过的:

private void comboBox1_SelectedItem(object sender, EventArgs e)
{
    MessageBox.Show("Populating form");
    m_dbConnection.Open();

    DataTable dt = new DataTable();
    SQLiteCommand command = m_dbConnection.CreateCommand();
    command.CommandText = "select * from rdpdirectory where company = @company order by company asc";
    //
    command.Parameters.Add(new SQLiteParameter("@company", comboBox1.SelectedText));
    SQLiteDataAdapter db = new SQLiteDataAdapter(command);
    db.Fill(dt);
    //return dt;

    DataColumn[] keyColumns = new DataColumn[1];
    keyColumns[0] = dt.Columns["company"];
    dt.PrimaryKey = keyColumns;

    DataRow row = dt.Rows.Find("company");
    if (row != null)
        txtCompany.Text = row["company"].ToString();
}   

【问题讨论】:

  • 谁能告诉我一个更好的方法??

标签: c# .net winforms sqlite datatable


【解决方案1】:

我用下面的代码解决了:

private void comboBox1_SelectedItem(object sender, EventArgs e)
        {
            MessageBox.Show("Populating form");
            m_dbConnection.Open();

            DataTable dt = new DataTable();
            SQLiteCommand command = m_dbConnection.CreateCommand();
            command.CommandText = "select * from rdpdirectory where company = @company order by company asc";
            //
            command.Parameters.Add(new SQLiteParameter("@company", comboBox1.SelectedText));
            SQLiteDataAdapter db = new SQLiteDataAdapter(command);
            db.Fill(dt);
            //return dt;


            DataColumn[] keyColumns = new DataColumn[1];
            keyColumns[0] = dt.Columns["company"];
            dt.PrimaryKey = keyColumns;

            DataRow row = dt.Rows[0];
            txtCompany.Text = row["company"].ToString();
            txtServer.Text = row["server"].ToString();
            txtUserName.Text = row["username"].ToString();
            txtPassword.Text = row["password"].ToString();
        }

【讨论】:

    猜你喜欢
    • 2015-06-20
    • 1970-01-01
    • 1970-01-01
    • 2020-03-17
    • 1970-01-01
    • 1970-01-01
    • 2018-02-27
    • 2012-08-11
    • 2021-05-19
    相关资源
    最近更新 更多