1

このドキュメントを読んで、次のコードが機能するという印象を受けました。

    using (SqlConnection con = new SqlConnection(Properties.Settings.Default.C_Str))
    {
        try
        {
            using (SqlCommand cmd = new SqlCommand("PerformSearch", con))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                string SearchString = textBoxSearchSting.Text;
                cmd.Parameters.Add("SearchString", SqlDbType.VarChar).Value = SearchString;

                con.Open();

                dataGridView1.DataSource = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            }
        }
        catch (Exception) { throw; }
        finally { con.Close(); }
    }

または、少なくとも次のものを使用できます。

        using (SqlConnection con = new SqlConnection(Properties.Settings.Default.C_Str))
        {
            try
            {
                using (SqlCommand cmd = new SqlCommand("PerformSearch", con))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    string SearchString = textBoxSearchSting.Text;
                    cmd.Parameters.Add("SearchString", SqlDbType.VarChar).Value = SearchString;

                    con.Open();

                    SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                    dataGridView1.DataSource = reader;
                }
            }
            catch (Exception) { throw; }
            finally { con.Close(); }
        }

しかし、上記の作業のいずれもありません。以下のコードを使用すると、同じ検索値で機能しますが、データグリッドビューにクエリを入力する最良の方法は以下ですか? そして、上記のどちらも機能しないのはなぜですか?

        using (SqlConnection con = new SqlConnection(Properties.Settings.Default.C_Str))
        {
            try
            {
                using (SqlCommand cmd = new SqlCommand("PerformSearch", con))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    string SearchString = textBoxSearchSting.Text;
                    cmd.Parameters.Add("SearchString", SqlDbType.VarChar).Value = SearchString;

                    con.Open();                           

                    DataTable dt = new DataTable();
                    dt.Load(cmd.ExecuteReader(CommandBehavior.CloseConnection));

                    if (dt.Rows.Count > 0 && dt != null)
                    {
                        dataGridView1.DataSource = dt;
                    }
                }
            }
            catch (Exception) { throw; }
            finally { con.Close(); }
        }

以下も機能しますが、何らかの理由でデータアダプターを使用したほうがよいでしょうか?

        using (SqlConnection con = new SqlConnection(Properties.Settings.Default.C_Str))
        {
            try
            {
                using (SqlCommand cmd = new SqlCommand("PerformSearch", con))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    string SearchString = textBoxSearchSting.Text;
                    cmd.Parameters.Add("SearchString", SqlDbType.VarChar).Value = SearchString;

                    DataTable dt = new DataTable();

                    using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                    {
                        con.Open();

                        da.Fill(dt);

                        if (dt.Rows.Count > 0 && dt != null)
                        {
                            dataGridView1.DataSource = dt;
                        }
                    }
                }
            }
            catch (Exception) { throw; }
            finally { con.Close(); }
        }
4

0 に答える 0