如何从方法返回3个数据集值? [英] How to return 3 dataset values from a method?

查看:73
本文介绍了如何从方法返回3个数据集值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我使用以下函数返回一个数据集的值。



现在我有3个数据集(listSheetNames [0],listSheetNames [1],listSheetNames [2])

Hi,

I am using the following function to return the values of one dataset.

Now I have 3 datasets(listSheetNames[0],listSheetNames[1],listSheetNames[2])

private DataSet GetExcelData(string ExcelPath, string ExcelFileName)
        {
            object misValue = System.Reflection.Missing.Value;
            Excel.Application xlApp = new Excel.Application();
            Excel.Workbook xlWorkBook = xlApp.Workbooks.Add(misValue);
            Excel.Worksheet xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
            Excel._Worksheet xlWorksheet = xlWorkBook.Sheets[1];
            Excel.Range xlRange = xlWorksheet.UsedRange;


            string Provider = string.Empty;
            string ExtendedProperties = string.Empty;
            List<string> listSheetNames = new List<string>();
            DataSet dataSet1 = new DataSet();
            DataSet dataSet2 = new DataSet();
            DataSet dataSet3 = new DataSet();
            OleDbConnection connection = null;
            try
            {
                // for 97-03 Excel file
                if (Path.GetExtension(ExcelFileName).Equals(".xls"))
                {
                    Provider = "Microsoft.Jet.OLEDB.4.0";
                    ExtendedProperties = "Excel 8.0;HDR=Yes;IMEX=1";
                }
                // for 2007 Excel file
                else if (Path.GetExtension(ExcelFileName).Equals(".xlsx"))
                {
                    Provider = ".xlsx";
                    ExtendedProperties = "Excel 12.0;HDR=Yes;IMEX=1";
                }
                try
                {
                    connection = new OleDbConnection("provider =" + Provider + "; Data Source='" + ExcelPath + ExcelFileName + "';" + "Extended Properties=\"" + ExtendedProperties + "\";");
                    connection.Open();
                }
                catch (Exception ex)
                {
                    throw new Exception("Error in opening the file " + ExcelFileName + "\nDetails: " + ex.Message);
                }

                listSheetNames = GetExcelSheetName(connection);
               OleDbDataAdapter command1 = new OleDbDataAdapter("Select * From [" + listSheetNames[1] + "]", connection);
               //OleDbDataAdapter command2 = new OleDbDataAdapter("Select * From [" + listSheetNames[1] + "]", connection);
               //OleDbDataAdapter command3 = new OleDbDataAdapter("Select * From [" + listSheetNames[1] + "]", connection);
                //0=green,1=red,2=yellow
                command1.Fill(dataSet1);
                //command2.Fill(dataSet2);
                //command3.Fill(dataSet3);
                connection.Close();

               
            }
            catch (Exception ex)
            {
                throw new Exception("Error in retrieving data from the file " + ExcelFileName + "\nDetails: " + ex.Message);
            }
            return dataSet1;
        }





如何制作一个方法来分别返回所有3个数据集?



How can I make a method to return all the 3 datasets separately???

推荐答案

一种可能的解决方案,因为DataSet是引用类型,就是将它们用作函数参数;将GetExcelData描述更改为:

One of possible solutions, since DataSet is reference type, is to use them as function parameters; change you GetExcelData description to:
private void GetExcelData
    (string ExcelPath, DataSet ds1, DataSet ds2, DataSet ds3){//Code ommited}







//Before calling GetExcelData;
DataSet ds1 = new DataSet();
DataSet ds2 = new DataSet();
DataSet ds3 = new DataSet();

//Now call function to fill datasets:
GetExcelData("Path", ds1, ds2, ds3);





您的数据集将填充数据。



And your datasets will be filled with data.


使用List执行任务



use List to do your task

List<DataSet> lstDS = new List<DataSet>();
lstDS.Add(dataSet1);
lstDS.Add(dataSet2);
lstDS.Add(dataSet3);

return lstDS;


这篇关于如何从方法返回3个数据集值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆