【问题标题】:How to get the datatable with selected rows from stored procedure?如何从存储过程中获取包含选定行的数据表?
【发布时间】:2022-11-25 03:35:16
【问题描述】:

我从存储过程中得到了 8 行列名。我把它读作数据表。但是,我只想要参考employeeID 的三列​​。

这是我运行程序后得到的表格。

我将其视为数据表。但是,我只想选择三行并将这三行存储在数据表中。逻辑是 if EmployeeID == "1A-111" and "1A-113" and "1A-114" 然后根据它获得三行并将其放回数据表。我不知道该怎么做。

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 the stored procedure 1 called load_ooc()
        private DataTable load_ooc()
        {
            // stored 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;
        }
    }
}

请建议我能做什么。谢谢你。

【问题讨论】:

  • 你的存储过程在哪里?
  • 请不要发布图片,使用格式化文本。顺便说一句,第三行中CompleteDate 的日期不是有效日期

标签: c# sql-server stored-procedures datatable datarow


【解决方案1】:

您可以使用数据视图。

DataView dv = new DataView(yourDatatable);
dv.RowFilter = "query"; // query example: "EmployeeID = '1A-111' or EmployeeID = '1A-113' or EmployeeID = '1A-114'"

How to apply filter to DataView with Multiple "AND" conditions

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-21
    • 1970-01-01
    • 2017-10-24
    • 2016-08-14
    • 2014-04-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多