简体   繁体   English

如何将DataTable传递回调用方法?

[英]How do I pass a DataTable back to the calling method?

I have been trying to pass a DataTable back to the calling method. 我一直在尝试将DataTable返回给调用方法。 Here is the code that calls the Class to create the DataTable . 这是调用Class创建DataTable的代码。

string SQL = "SELECT .....";
Tables.Create_DataTable(SQL);

Here is the code for the class Tables . 这是Tables类的代码。

public static class Tables
{
    internal static object Create_DataTable(string SQL)
    {
        DataTable dataTable = new DataTable("Work_Orders");

        using (DataAccessClass.sql_Connection)
        using (SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(SQL, DataAccessClass.sql_Connection))
        {
            DataAccessClass.OpenConnection();

            sqlDataAdapter.Fill(dataTable);
        }

        return Work_Orders;
    }
}

Right after the sqlDataAdapter.Fill(dataTable); 就在sqlDataAdapter.Fill(dataTable); there is a Work_Orders DataTable containing the requested data. 有一个包含所请求数据的Work_Orders DataTable On the return, I realize that the DataTable is actually an object . 在返回时,我意识到DataTable实际上是一个object This is where I get stuck. 这就是我卡住的地方。 How do I change the object to a DataTable ? 如何将object更改为DataTable Then I will add it to the DataSet . 然后,将其添加到DataSet

You should return dataTable from your method, but as the basic question the signature should be : 您应该从您的方法返回dataTable ,但是作为基本问题,签名应该是:

attributes modifiers return-type method-name(parameters )
    {
     statements
    }

Take a look at Here or Here 看看这里这里

But change your method like: 但是像这样更改您的方法:

internal static DataTable Create_DataTable(string SQL)
{
    DataTable dataTable = new DataTable("Work_Orders");

    using (DataAccessClass.sql_Connection)
    using (SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(SQL, DataAccessClass.sql_Connection))
    {
        DataAccessClass.OpenConnection();

        sqlDataAdapter.Fill(dataTable);
    }

    return dataTable;
}

EDIT: 编辑:

1- Avoid to return Object from a method 1-避免从方法中返回对象

2- Avoid returning DataTable from a method also 2-避免也从方法返回DataTable

because both of them make your code unreadable and unclean, so this is better to return an object which specified for example suppose your DataTable have some fileds about user then this better to be like: 因为它们都使您的代码不可读和不干净,所以最好返回一个指定的对象,例如,假设您的DataTable中有一些关于用户的文件,则最好这样:

if DataTable have one row: 如果DataTable有一行:

internal static User Create_DataTable(string SQL)
{
    DataTable dataTable = new DataTable("Work_Orders");

    using (DataAccessClass.sql_Connection)
    using (SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(SQL, DataAccessClass.sql_Connection))
    {
        DataAccessClass.OpenConnection();

        sqlDataAdapter.Fill(dataTable);
    }
     User u = new User()
       { 
          Name = dataTable.Rows[0]("Name"),
          Family = dataTable.Rows[0]("Family"),
          UserName = dataTable.Rows[0]("UserName")
        }
    return u;
}

or if DataTable have infos about several users (Have some rows): 或者,如果DataTable具有有关多个用户的信息(具有一些行):

internal static List<User> Create_DataTable(string SQL)
{
    DataTable dataTable = new DataTable("Work_Orders");

    using (DataAccessClass.sql_Connection)
    using (SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(SQL, DataAccessClass.sql_Connection))
    {
        DataAccessClass.OpenConnection();

        sqlDataAdapter.Fill(dataTable);
    }
     List<User> uList = new List<User>();
     foreach (DataRow row in Datatable.Rows) 
     { 
         uList.Add(new User()
              { 
                   Name = row["Name"],
                   Family = row["Family"],
                   UserName = row["UserName"]
               });
      }
    return uList;
}

The above code just are samples. 上面的代码只是示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM