【问题标题】:How to convert datatable to list<> or array?如何将数据表转换为列表<> 或数组?
【发布时间】:2015-08-10 17:35:12
【问题描述】:

我想使用下面的代码从表中获取所有数据:

            CameraBUS cameraBus = new CameraBUS();   //Contain class provider access to DAO for GetByAllCamera.

            DataTable dt = cameraBus.Camera_GetByAllCamera();   //select * from table Camera

            foreach (DataRow row in dt.Rows)
            {
                CameraDTO camera = new CameraDTO(row);
                if(camera.IDKhuVuc == 4)
                {
                    OpenCamera(camera.ViTri, camera.TaiKhoan, camera.MatKhau, camera.IP);     
                }

                if(camera.IDKhuVuc == 3)
                {
                    OpenCamera(camera.ViTri, camera.TaiKhoan, camera.MatKhau, camera.IP);
                }
            }

如果 camera.IDKhuVuc 有 7 行或更多行。它总是启动 StartThead1 并且我的程序停止工作。 示例:

我的数据有 4 行,但此代码无法获取 4 行 4 个摄像头。它只打开第一个和最后一个摄像头。

当我调试我的程序时,它运行 2 行(第 2、3 行)。但是当我运行我的程序时,它并没有打开摄像头 2 和摄像头 3。

我想我应该使用 List 或数组。我该如何解决这个问题?

班级OpenCamera():

        private void OpenCamera(bool position, string username, string password, string ipAddress)
        {
            if (!position)
            {
                axLiveX1.IpAddress = ipAddress;
                axLiveX1.UserName = username;
                axLiveX1.Password = password;
                axLiveX1.DataPort = 5550;
                axLiveX1.CommandPort = 4550;
                axLiveX1.AudioDataPort = 6550;
                axLiveX1.DefaultCam = 8;
                axLiveX1.OnGetPicture += new AxLIVEXLib._DLiveXEvents_OnGetPictureEventHandler(axLiveX1_OnGetPicture);
                axLiveX1.AutoLogin = true;
                if (axLiveX1.Connect())
                {

                }
                axLiveX1.StartGrapImage(true);

                axLiveX2.IpAddress = ipAddress;
                axLiveX2.UserName = username;
                axLiveX2.Password = password;
                axLiveX2.DataPort = 5550;
                axLiveX2.CommandPort = 4550;
                axLiveX2.AudioDataPort = 6550;
                axLiveX2.DefaultCam = 8;
                axLiveX2.OnGetPicture += new AxLIVEXLib._DLiveXEvents_OnGetPictureEventHandler(axLiveX2_OnGetPicture);
                axLiveX2.AutoLogin = true;
                axLiveX2.Connect();
                axLiveX2.StartGrapImage(true);
            }
            else
            {
                axLiveX3.IpAddress = ipAddress;
                axLiveX3.UserName = username;
                axLiveX3.Password = password;
                axLiveX3.DataPort = 5550;
                axLiveX3.CommandPort = 4550;
                axLiveX3.AudioDataPort = 6550;
                axLiveX3.DefaultCam = 8;
                axLiveX3.OnGetPicture += new AxLIVEXLib._DLiveXEvents_OnGetPictureEventHandler(axLiveX3_OnGetPicture);
                axLiveX3.AutoLogin = true;
                axLiveX3.Connect();
                axLiveX3.StartGrapImage(true);

                axLiveX4.IpAddress = ipAddress;
                axLiveX4.UserName = username;
                axLiveX4.Password = password;
                axLiveX4.DataPort = 5550;
                axLiveX4.CommandPort = 4550;
                axLiveX4.AudioDataPort = 6550;
                axLiveX4.DefaultCam = 8;
                axLiveX4.OnGetPicture += new AxLIVEXLib._DLiveXEvents_OnGetPictureEventHandler(axLiveX4_OnGetPicture);
                axLiveX4.AutoLogin = true;
                axLiveX4.Connect();
                axLiveX4.StartGrapImage(true);
            }
        }

【问题讨论】:

    标签: c# list datatable


    【解决方案1】:

    试试这样的

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("Col A", typeof(int));
                dt.Columns.Add("Col B", typeof(int));
                dt.Columns.Add("Col C", typeof(int));
    
                dt.Rows.Add(new object[] { 1, 2, 3});
                dt.Rows.Add(new object[] { 10, 20, 30 });
                dt.Rows.Add(new object[] { 100, 200, 300 });
    
                List<DataRow> rows = dt.AsEnumerable().Select(x => x).ToList();
                var numbers = dt.AsEnumerable().Select(x => x.ItemArray).ToList();
    
            }
        }
    }
    ​
    ​
    

    您可以使用原始代码获取任何这样的单元格的数据

                foreach (DataRow row in dt.Rows)
                {
                    string colA = (string)row["Col A"];
                }​
    

    要获取整列,请尝试这样的操作

               var colA = dt.AsEnumerable().Select(x => x.Field<string>("Col A")).ToList();​
    

    【讨论】:

    • 谢谢。如果我使用这样的列表。如何获取 1 行或 1 列的值?我调试了一个dt 变量保存表中的所有值。我编辑我的代码:pastebin.com/dy7vtVdn 我不知道获取值来执行 if 语句。非常感谢
    • GetOnePicture 不应在 OpenCamera() 方法中。您需要另一种方法来获取单张图片。返回图像。您的 OpenCamera() 没有返回参数 (void)。
    • OpenCamera() 方法中没有任何可以返回的错误条件。我会添加一个异常处理程序,以便在发生异常时返回错误条件。我还假设当打开失败时 connect 方法会返回一个值。
    • 没有关于 AxLiveXx 控件的任何文档。
    • 只得到网页的顶层。只是索引没有内容。必须保存为平面文件或压缩整个文件夹。
    猜你喜欢
    • 2015-08-06
    • 2021-09-29
    • 1970-01-01
    • 1970-01-01
    • 2019-10-04
    • 2013-06-10
    • 1970-01-01
    • 2013-02-14
    • 1970-01-01
    相关资源
    最近更新 更多