简体   繁体   English

C#从表中读取行并将值存储在字符串中[]

[英]C# Read rows from a table and store the values in a string[]

I am using C# and ASP.NEt to create a web application where I need to insert/view data. 我正在使用C#和ASP.NEt创建一个需要插入/查看数据的Web应用程序。 I created my own class that stores values for a "Ticket" or a support ticket. 我创建了自己的类,用于存储“票证”或支持票证的值。 My question is, how can I grab the values from the SQL table with the tickets and store them in the Ticket class? 我的问题是,如何从带有票证的SQL表中获取值并将其存储在票证类中? I don't need the specifics, just what SQL class I should use and how to store them. 我不需要细节,只需要使用什么SQL类以及如何存储它们。

Thanks! 谢谢!

Several options: ADO.NET to query the data using the SqlConnection and SqlDataAdapter, filling the results for a DataTable, and copying the data row data to your Ticket class. 几个选项:ADO.NET使用SqlConnection和SqlDataAdapter查询数据,填充DataTable的结果,并将数据行数据复制到Ticket类。

Or even better, use LINQ to SQL or ADO.NET Entity Framework. 甚至更好的是,使用LINQ to SQL或ADO.NET实体框架。 Plenty of examples of all of this online. 所有这些在线示例。

This tutorial should get you started: http://www.csharp-station.com/Tutorials/ADODotNet/Lesson02.aspx 本教程将帮助您入门: http : //www.csharp-station.com/Tutorials/ADODotNet/Lesson02.aspx

In the section that reads: 在以下部分中:

while (rdr.Read())
{
     Console.WriteLine(rdr[0]);
}

You will want replace the Console.WriteLine() with code to to create new instances of Ticket and add them to a ticket list. 您将需要用代码替换Console.WriteLine()来创建Ticket的新实例并将其添加到票据列表中。

您应该能够使用SqlDataReader来获取数据,获取列数据并将其放入字符串数组。

Have you considered using an ORM? 您是否考虑过使用ORM? Entity Framework is one the most used. 实体框架是最常用的一种。

See this tutorial for more information: 有关更多信息,请参见本教程:

http://www.codeproject.com/Articles/363040/An-Introduction-to-Entity-Framework-for-Absolute-B http://www.codeproject.com/Articles/363040/An-Introduction-to-Entity-Framework-for-Absolute-B

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

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