我需要获取列类型并将其插入表(标签)

时间:2019-07-11 09:11:00

标签: c# winforms

我正在制作一个windows form application,它可以处理SQL server中的数据。我正在尝试获取列类型并将其放在表中,以便可以像这样(Label{columnName}{columnType} Textbox)。 查询是:

SELECT DATA_TYPE 
FROM INFORMATION_SCHEMA.COLUMNS 
WHERE TABLE_NAME = 'comboBox1.SelectedItem' 
AND  COLUMN_NAME = '{labels[i].Text}'

我只需要了解如何将该查询放入带有结果的值中并将其添加到我的tableLayout中。

立即尝试,但未执行

using (form1.conn = new SqlConnection($"Server = {form1.ServerBox.Text }; Database = { form1.DBBox.Text}; Trusted_Connection = True"))
{
    form1.conn.Open();
    using (cmd = new SqlCommand(getSqlTypes, form1.conn))
    {
        using (SqlDataReader reader = cmd.ExecuteReader())
        {
            while (reader.Read())
            {
                labels[i] = new Label();
                labels[i].Text = (string)reader["TABLE_NAME"];
                tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.AutoSize));
                tableLayoutPanel.SetCellPosition(labels[i], new TableLayoutPanelCellPosition(2, k++));
            }
        }
    }

    form1.conn.Close();
}

1 个答案:

答案 0 :(得分:1)

发生以下代码:

SqlDataAdapter sqladapter = new SqlDataAdapter($"{getSqlTypes}", form1.conn = new SqlConnection($"Server = {form1.ServerBox.Text }; Database = { form1.DBBox.Text}; Trusted_Connection = True"));
                DataSet dt = new DataSet();
                sqladapter.Fill(dt);
                labels[i] = new Label();
                labels[i].Text = dt.Tables[0].Rows[0][0].ToString();
                tableLayoutPanel.SetCellPosition(labels[i], new TableLayoutPanelCellPosition(2, k++));
                tableLayoutPanel.Controls.Add(labels[i]);
                getSqlTypes = $"SELECT DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '{comboBox1.SelectedItem}' AND COLUMN_NAME = ";