1

私はC#に非常に慣れておらず、SQLサーバーデータベースの選択されたテーブルのインデックスを再作成するツールを開発しようとしているため、ユーザーはデータベースにアクセスしてコマンドを実行する必要がありません。

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

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        public DataSet GetTableNames()
        {
            try
            {

                //Local variables
                DataSet resultSet = new DataSet();

                //Get the connection string from the config file
                string connectionString = ConfigurationManager.ConnectionStrings["DBTool.Properties.Settings.ConStr"].ConnectionString;

                //Create a new database connection
                SqlConnection connection = new SqlConnection(connectionString);

                //Set the stored procedure 'sproc_Get_Tables_Names' as the command to be executed
                using (SqlCommand cmd = new SqlCommand("sproc_Get_Tables_Names", connection))
                {

                    //Setup the command object
                    SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandTimeout = 0;

                    //Open the connection
                    if (connection.State != ConnectionState.Open)
                    {
                        connection.Open();
                    }

                    //Execute the query and fill out the dataset
                    adapter.Fill(resultSet);

                    //Close the connection
                    connection.Close();

                    //Return the result of the stored procedure
                    return resultSet;
                }
            }

            catch (Exception ex)
            {

               MessageBox.Show("An error occurred due to the following exception: " + ex.ToString() + ".","Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
               return null;

            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            DataSet set = GetTableNames();

            dataGridView1.DataSource = set.Tables[0];
        }

        private void dataGridView1_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
        {
        }
    }
}
4

1 に答える 1

1

私のデータベースの 1 つに対して (別のストアド プロシージャを使用して) コードを実行したところ、動作しました。例外はありますか?接続文字列のタイムアウトを低い値に設定して、もう一度実行してみてください。

コードが機能するという事実は、SQL Server/Network Connectivity/Permissions に問題があり、タイムアウトが長すぎて例外が返されないことを意味します。

試してみるいくつかのこと:

  • ストアド プロシージャはサーバー上で正常に実行されますか?

  • もしそうなら、それはどのくらいのデータを返していますか? ばかげた量のデータを返していて、時間がかかっていますか?

  • 接続文字列によって指定されたユーザーは、ストアド プロシージャを実行する権限を持っていますか?

  • 同じ資格情報を使用して、SQL Management Studio を使用して SQL Server に接続できますか?

于 2013-08-20T10:08:25.077 に答える