简体   繁体   中英

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. 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? I don't need the specifics, just what SQL class I should use and how to store them.

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.

Or even better, use LINQ to SQL or ADO.NET Entity Framework. Plenty of examples of all of this online.

This tutorial should get you started: 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.

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

Have you considered using an 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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