【解決方法】ストアドプロシージャから選択した行でデータテーブルを取得する方法を教えてもらえますか?


そのため、ストアド プロシージャから列名を持つ 8 行を取得しました。 これを DataTable と読みます。 ただし、employeeID を参照する列は 3 つだけ必要です。

C#
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Reflection.Emit;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Button = System.Windows.Forms.Button;
 
namespace CrossCrackApp
{
 
 
 
    public partial class Form1 : Form
    {
        public object DBManager { get; private set; }
 
        public string strConn = ConfigurationManager.AppSettings["CIM.ReportDataDB"];
 
        
        public int thresh_val = Convert.ToInt32(ConfigurationManager.AppSettings["thresh_val"]);
 
       
        public Form1()
        {
 
            InitializeComponent();
 
        }
 
 
        private void Form1_Load(object sender, EventArgs e)
 
        {
            display_cross_crack();             
            alarm_trigger();                  
            Show_Only_Respective_Lbl_Btn();   
            timer();                        
            lbl_timer();
            Console.WriteLine(thresh_val);
 
 
        }
 
 
        //This is Stored Procedure 1 called load_ooc()
        private DataTable load_ooc()
        {
 
            //Store procedure
 
            SqlConnection _con = new SqlConnection(strConn);
            DataTable rdr = new DataTable();
 
            try {
                _con.Open();
                lbl_error.Text = (_con is null) ? "Database not Connected" : "Database Connected";
                //create command
                SqlCommand cmd = _con.CreateCommand();
 
            //specify stored procedure to execute
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "LastHourCrossCrack";
 
            //execute command
           
            rdr.Load(cmd.ExecuteReader());
 
            //This is where the table is read I think so
            //Need To DO
            //Select the rows referenced to ID column
            //Then put it back to DataTable for the selected rows
            //return rdr now but need to change back to DataTable that only has selected rows
 
            //Trial & Errors
            //rdr.Select("")
            //if { rdr.Columns }
 
            }
            catch(Exception ex)
            {
                lbl_error.Text = ex.Message.ToString().Substring(0,100);
 
                lbl_error.ForeColor = Color.Red;
            }
            finally
            {
 
                if (_con == null)
                {
                    Console.WriteLine("Connection To Database is not succesful.");
                }
                
                _con.Dispose();
            }
            return rdr;
        }

私が試したこと:

DataRow select を試して DataTable に戻しましたが、エラーが発生しました

解決策 1

あなたのストアド プロシージャが何をするのか、どのパラメータを受け入れるのか、私たちにはわかりません。

しかし … 特定の ID を持つ行のみを要求するには、WHERE 句を使用します。

C#
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand cmd = new SqlCommand("SELECT Age, Description FROM myTable WHERE ID = @ID", con))
        {
        cmd.Parameters.AddWithValue("@ID", myTextBox.Text);
        using (SqlDataReader reader = cmd.ExecuteReader())
            {
            while (reader.Read())
                {
                int age = (int) reader["Age"];
                string desc = (string) reader["Description"];
                Console.WriteLine($"{age}\n{desc}");
                }
            }
        }
    }

またはデータテーブルの場合:

using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlDataAdapter da = new SqlDataAdapter("SELECT Age, Description FROM myTable WHERE ID = @ID", con))
        {
        da.SelectCommand.Parameters.AddWithValue("@ID", myTextBox.Text);
        DataTable dt = new DataTable();
        da.Fill(dt);
        myDataGridView.DataSource = dt;
        }
    }

コメント

タイトルとURLをコピーしました