在c#中从DataTable中捕获数据

Catch data from DataTable in c#

SqlCommand detailsCommand = new SqlCommand("SELECT bd.bookName, bd.authorName, bd.publishDate bc.description, bd.keyWords FROM bookCart as bc, bookDetails as bd WHERE bc.bookCode = bd.code");

DataTable detailsTable = book.showData(detailsCommand);

String BookName = detailsTable.Rows[0]["bookName"].ToString();
String AuthorName = detailsTable.Rows[1]["authorName"].ToString();
String PublishDate = detailsTable.Rows[2]["publishDate"].ToString();
String Describe = detailsTable.Rows[3]["describtion"].ToString();
String KeyWords = detailsTable.Rows[4]["keyWords"].ToString();

它在最后两行中给了我一个 IndexOutOfRangeException

我猜是因为描述在另一个table中,但我无法解决,你能帮我解决这个问题吗?

下一步试试:

String BookName = detailsTable.Rows[0]["bookName"].ToString();
String AuthorName = detailsTable.Rows[0]["authorName"].ToString();
String PublishDate = detailsTable.Rows[0]["publishDate"].ToString();
String Describe = detailsTable.Rows[0]["description"].ToString();
String KeyWords = detailsTable.Rows[0]["keyWords"].ToString();

DataTable.Rows contains the collection of rows that belong to this table, so detailsTable.Rows[0] is a first row, detailsTable.Rows[1] is second one and so on (in your case it seems there are 3 of them, so you get IndexOutOfRangeException第4个)