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接口也没有用。
答案 0 :(得分:2)
在for循环中,您仅打印整个数据集。因此应该打印数据集。
您需要替换以下内容-
Console.Write(a.Rezultate)
与
Console.WriteLine(row[column.ColumnName].ToString());