从 SQL Server Express 数据库中读取表格

Reading tables from SQL Server Express database

我正在尝试读取 SQL Server Express 数据库中包含的表格,但它总是空着。哪里做错了?

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    string connectionString = "Data Source=LPMSW09000012JD\SQLEXPRESS;Initial Catalog=Pharmacies;Integrated Security=True";

    string query = "SELECT * FROM INFORMATION_SCHEMA.TABLES";

    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        SqlCommand command = new SqlCommand(query, connection);
        command.Connection.Open();

        command.ExecuteNonQuery();
    }
}

已更新但仍为空:

  private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        try {

            string connectionString = "Data Source=LPMSW09000012JD\SQLEXPRESS;Initial Catalog=Pharmacies;Integrated Security=True";
            SqlConnection con2 = new SqlConnection(connectionString);
            con2.Open();
            string query = "SELECT * FROM INFORMATION_SCHEMA.TABLES";
            SqlCommand cmd2 = new SqlCommand(query, con2);

            SqlDataReader dr2 = cmd2.ExecuteReader();
            while (dr2.Read())
            {

                comboBox1.Items.Add((string)dr2[0]);

            }


        }


        catch (Exception ex)
        {

            MessageBox.Show(ex.ToString());
        }}

最新更新代码。想我想通了,但仍然显示为空:

 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        try {

            string connectionString = "Data Source=LPMSW09000012JD\SQLEXPRESS;Initial Catalog=Pharmacies;Integrated Security=True";
            SqlConnection con2 = new SqlConnection(connectionString);
            con2.Open();
            string query = "SELECT * FROM INFORMATION_SCHEMA.TABLES ";
            SqlCommand cmd2 = new SqlCommand(query, con2);

            SqlDataReader dr2 = cmd2.ExecuteReader();
            while (dr2.Read())
            {

                string Dtables = dr2.GetString(dr2.GetOrdinal("TABLE_NAME"));
                comboBox1.Items.Add(Dtables);

            }

        }

        catch (Exception ex)
        {

            MessageBox.Show(ex.ToString());
        }}

完整 class:

    namespace unique_database{ public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {

    }

    private void button2_Click(object sender, EventArgs e)
    {
        string connectionString = "Data Source=LPMSW09000012JD\SQLEXPRESS;Initial Catalog=Pharmacies;Integrated Security=True";
        string query = "CREATE TABLE [dbo].[" + textBox1.Text + "](" + "[Code] [varchar] (13) NOT NULL," +
       "[Description] [varchar] (50) NOT NULL," + "[NDC] [varchar] (50) NULL," +
        "[Supplier Code] [varchar] (38) NULL," + "[UOM] [varchar] (8) NULL," + "[Size] [varchar] (8) NULL,)";


        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            SqlCommand command = new SqlCommand(query, connection);
            command.Connection.Open();
            command.ExecuteNonQuery();
        }

        SqlConnection con = new SqlConnection("Data Source=LPMSW09000012JD\SQLEXPRESS;Initial Catalog=Pharmacies;Integrated Security=True");
        string filepath = textBox2.Text; //"C:\Users\jdavis\Desktop\CRF_105402_New Port Maria Rx.csv";
        StreamReader sr = new StreamReader(filepath);
        string line = sr.ReadLine();
        string[] value = line.Split(',');
        DataTable dt = new DataTable();
        DataRow row;
        foreach (string dc in value)
        {
            dt.Columns.Add(new DataColumn(dc));
        }

        while (!sr.EndOfStream)
        {
            value = sr.ReadLine().Split(',');
            if (value.Length == dt.Columns.Count)
            {
                row = dt.NewRow();
                row.ItemArray = value;
                dt.Rows.Add(row);
            }
        }
        SqlBulkCopy bc = new SqlBulkCopy(con.ConnectionString, SqlBulkCopyOptions.TableLock);
        bc.DestinationTableName = textBox1.Text;
        bc.BatchSize = dt.Rows.Count;
        con.Open();
        bc.WriteToServer(dt);
        bc.Close();
        con.Close();

    }

    private void button3_Click(object sender, EventArgs e)
    {
        OpenFileDialog openFileDialog = new OpenFileDialog();
        openFileDialog.Filter = "CSV files (*.csv)|*.csv|XML files (*.xml)|*.xml";


        if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            this.textBox2.Text = openFileDialog.FileName;
        }
    }

    private void FillCombo()
    {
        try
        {

            string connectionString = "Data Source=LPMSW09000012JD\SQLEXPRESS;Initial Catalog=Pharmacies;Integrated Security=True";
            using (SqlConnection con2 = new SqlConnection(connectionString))
            {
                con2.Open();
                string query = "SELECT * FROM INFORMATION_SCHEMA.TABLES ";
                SqlCommand cmd2 = new SqlCommand(query, con2);

                SqlDataReader dr2 = cmd2.ExecuteReader();
                while (dr2.Read())
                {
                    int col = dr2.GetOrdinal("TABLE_NAME");
                    comboBox1.Items.Add(dr2[col].ToString());
                }

            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }

使用 ExecuteReader() 而不是 ExecuteNonQuery()。然后使用 SqlDataReader() 获取结果。

好的,首先,不要用comboBox1_SelectedIndexChanged来填充comboBox1。 使用 YourForm.OnLoad 事件、表单构造函数或单击按钮,但不要使用 SelectedIndexChanged 来填充自身,因为每次 select 的一项 comboBox1.

要获取列索引,只需使用:dr2.GetOrdinal("TABLE_NAME");

private void FillCombo()
{
    try 
    {
        string connectionString = "Data Source=LPMSW09000012JD\SQLEXPRESS;Initial Catalog=Pharmacies;Integrated Security=True";
        using (SqlConnection con2 = new SqlConnection(connectionString))
        {
            con2.Open();
            string query = "SELECT * FROM INFORMATION_SCHEMA.TABLES";
            SqlCommand cmd2 = new SqlCommand(query, con2);

            SqlDataReader dr2 = cmd2.ExecuteReader();
            while (dr2.Read())
            {
                int col = dr2.GetOrdinal("TABLE_NAME");
                comboBox1.Items.Add(dr2[col].ToString());
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
}