简体   繁体   English

从 SQL Server Express 数据库读取表

[英]Reading tables from SQL Server Express database

I am trying to read my tables that are contained within my database in SQL Server Express, but it keeps coming up empty.我试图在 SQL Server Express 中读取包含在我的数据库中的表,但它一直为空。 What is it that am doing wrong?我做错了什么?

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();
    }
}

Updated but still empty:已更新但仍为空:

  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());
        }}

Latest Updated code.最新更新的代码。 Think I figured it but still showing empty:认为我想通了,但仍然显示为空:

 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());
        }}

Full 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());
        }
    }

Ok, first, don't use comboBox1_SelectedIndexChanged to fill comboBox1 .好的,首先,不要使用comboBox1_SelectedIndexChanged来填充comboBox1 use YourForm.OnLoad event, the form constructor, or click a button, but don't use SelectedIndexChanged to fill itself, because you're executing this procedure every time you select one item of comboBox1 .使用YourForm.OnLoad事件、表单构造函数或单击按钮,但不要使用SelectedIndexChanged来填充自身,因为每次选择comboBox1一项时都会执行此过程。

To get column index simply use: dr2.GetOrdinal("TABLE_NAME");要获取列索引,只需使用: 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());
    }
}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM