[英]Instead of dataGridView in C# windows application, what should I use for console application to display a table from sql server?
Question as above. 问题如上。 I was told to use list<> to do so, but I still have no clue about how to do it exactly.
有人告诉我使用list <>这样做,但是我仍然不知道如何精确地做到这一点。 I am new to C#.
我是C#的新手。
You could try something like the very simplified example below... 您可以尝试以下非常简单的示例...
using System;
using System.Data;
using System.Data.SqlClient;
SqlConnection conn = new SqlConnection(***Your Connection String Here***);
conn.Open();
SqlDataAdapter adpt = new SqlDataAdapter("Select ID, Dish, Price From RestaurantMenu", conn);
DataTable dt = new DataTable();
adpt.fill(dt);
conn.Close();
foreach (DataRow dr in dt.Rows){
console.WriteLine(dr["ID"].ToString() + " | " + dr["Dish"].ToString() + " | " + dr["Price"].ToString());
}
Doing what jradich1234 did is the most practical approach. 做jradich1234所做的是最实用的方法。 You fill a dataset with the table data and then just go through it printing each line and then adding a \\n after the last column.
您用表数据填充数据集,然后通过它打印每一行,然后在最后一列之后添加\\ n。
If you really want to use the list<> object (or you have to do it), then the proccess has a little more of work. 如果您真的想使用list <>对象(或者必须这样做),那么该过程还有很多工作要做。 Basically you have to create the list and then, instead of printing, you add a new item to the list that represents the row... and then you move through the list and do exactly the same jradich1234 did.
基本上,您必须创建列表,然后而不是打印,而是向列表中添加一个代表该行的新项目...,然后在列表中移动并执行与jradich1234完全相同的操作。
It is a good way to practice the use of lists and simple structs, but it is not as efficiend as the previous answer. 这是练习使用列表和简单结构的好方法,但效率不如先前的答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.