如何将数据表转换为列表<>还是数组?

时间:2015-08-10 17:35:12

标签: c# list datatable

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

            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);
            }
        }

1 个答案:

答案 0 :(得分:0)

尝试这样的事情

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();​