C#代码返回System.Data.DataSet而不是数据本身?

时间:2018-12-10 18:29:23

标签: c#

public class Connect
{

    public DataSet Rezultate = new DataSet();
    public void Function(string query, string conexiune)
    {
        using (SqlConnection CONNECT = new SqlConnection(conexiune))
        {

            SqlDataAdapter adaptor = new SqlDataAdapter(query, CONNECT);
            SqlCommand command = new SqlCommand(query, CONNECT);
            try
            {

                command.Connection.Open();
                adaptor.Fill(Rezultate);


            }
            catch (Exception e)
            {
                Console.WriteLine("A aparut o eroare:" + e);
            }

        }
    }
}

class Mainclass
{
    private static void Main()
    {
        Connect a = new Connect();
        a.Function("select * from dbo.test", "Data Source=DESKTOP-S0FG0EC;Initial Catalog=AdventureWorks2016;Integrated Security=True");
        foreach (DataTable table in a.Rezultate.Tables)
        {
            foreach (DataRow row in table.Rows)
            {
                foreach (DataColumn column in table.Columns)
                {
                    Console.Write(a.Rezultate);
                    Console.ReadLine();
                }
            }
        }

    }

}

我想在Visual Studio控制台中显示查询的结果,但是它会返回System.Data.DataSet。我尝试了许多修复程序,但均无济于事。我尝试实现IEnumerable接口也没有用。

1 个答案:

答案 0 :(得分:2)

在for循环中,您仅打印整个数据集。因此应该打印数据集。

您需要替换以下内容-

    Console.Write(a.Rezultate) 

    Console.WriteLine(row[column.ColumnName].ToString());