简体   繁体   English

代替C#Windows应用程序中的dataGridView,我应该如何使用控制台应用程序显示来自sql server的表?

[英]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.

相关问题 如何在C#中将图像从SQL Server 2008 R2检索到Datagridview [Windows应用程序] - How to retrieve image from SQL Server 2008 R2 to Datagridview in C# [Windows Application] c#从控制台应用程序显示Windows窗体 - c# display windows form from console application 无法从C#中的客户端(控制台应用程序)通过WCF服务(已通过Windows身份验证)访问SQL Server - Unable to access SQL Server from WCF service (Windows authenticated) from client (console application) in C# C# Windows 窗体应用程序 - 在文本框中显示 sql server 值 - C# Windows Forms Application - display sql server values in textbox 从 SQL Server 代理(作业)运行 C# 控制台应用程序? - Run a C# Console Application from SQL Server Agent (Job)? C#在控制台应用程序中显示Sql表 - C# Show Sql table in console application 在单个datagridview C#.Net Windows应用程序中显示两个不同的表值 - Two different table values display in single datagridview C# .Net Windows Application 如何动态地在C#windows应用程序中的DataGridView单元格中添加表? - How to add a Table in a cell of DataGridView in C# windows application dynamically? 如何将 C# 控制台应用程序设置为使用默认控制台设置 (Windows Server 2012)? - How to set C# Console Application to use default console settings (Windows Server 2012)? 无法从Windows应用程序C#连接到SQL Server数据库 - unable to connect to sql server database from windows application c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM