main.cs

来自「< SQL Server2005程序设计>」· CS 代码 · 共 63 行

CS
63
字号
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;

namespace Chapter09
{
    public partial class frmMain : Form
    {
        // DataSet declared private so that btnExecute_Click and cboResultSets_SelectedIndexChanged can share it
        private DataSet ds;

        public frmMain()
        {
            InitializeComponent();
        }

        private void btnExecute_Click(object sender, EventArgs e)
        {
            // Cleanup/prep
            cboResultSets.Items.Clear();
            dgvResults.DataSource = null;
            ds = new DataSet();

            try
            {
                // Build DataAdapter based on user-supplied connection string and T-SQL query
                SqlDataAdapter da = new SqlDataAdapter(txtQuery.Text, txtConnectionString.Text);

                // Set Timeout to 5 minutes (300 seconds) to avoid timeout errors from debugging
                da.SelectCommand.CommandTimeout = 300;

                // Execute the commmand
                da.Fill(ds);

                // Load each DataTable's name into cboResultSets
                foreach (DataTable dtCurr in ds.Tables)
                {
                    cboResultSets.Items.Add(dtCurr.TableName);
                }

                // Select first result set (ds.Tables[0]) by default
                cboResultSets.SelectedIndex = 0;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void cboResultSets_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Display specific DataTable within ds.Tables, according to cboResultSets selection
            dgvResults.DataSource = ds.Tables[cboResultSets.SelectedIndex];
        }

    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?