开发者

Loading information from a database problem in c#

This is what i am trying to do. I have a database that i am reading from using the code:

OleDbCommand command;
command = new OleDbCommand("SELECT " + Student.ID + " FROM " + newStudent.Dat开发者_JAVA百科aFile, conn);
conn.Open();
dt.Load(command.ExecuteReader());
conn.Close();

I then have the datatable bind to a datagridview and display the contents of the table.Now the problem is, i have more information to add to the datatable dt that is not in the database. For example, i have a field for the student object called Grade that is not found in the datafile but entered in by the user and stored in a property for the student object.

Instead of loading the query result into a datatable, is there a way to load it into a list so i can manually create rows and columns for a datatable in another method and then add the contents of the list(containing id) and the grade information in the student object manually?


If you don't fancy going for a full blown ORM framework such as the one @Bas has suggested...

Take a look at the ToTable method available from on a Datatable's Dataview. You can get the DataView for your Datatable simply using DataTable.DefaultView:

List<Long> myList = dt.DefaultDataView.ToTable(True, "ID").AsEnumerable().ToList()
myList.Add(1234)
//etc

Alternatively, you can load the additional data you want to append into a second datatable, and use the DataTable.Merge Method

EDIT: To account for wanting to add additional columns, you can change the above list suggestion as follows:

// Create a class to hold the information you want to bind, 
// you could use anonymous types if preferred
class MyDataRow
{
    public long ID { get; set; }
    public string AnotherColumn { get; set; }
    public string AndAnotherColumn { get; set; }
}

// then later on when creating that list use something along the lines of:
List<MyDataRow> myList = dt.DefaultDataView.ToTable(True, "ID").AsEnumerable().Select(x => new MyDataRow { ID = x.ID }).ToList()
// you now have a list of MyDataRow which you can work with
// for example...
if (myList.Any())
    myList.First().AnotherColumn = "foo";

// as an exmaple of using an anoymous type (not my preference, but an option nonetheless)
var anonymousList = dt.DefaultDataView.ToTable(True, "ID").AsEnumerable().Select(x => new { ID = x.ID, whateverYouWantToCallIt = "some other data but this is read only property" }).ToList()
// you can work with the anonymous list in much the same way, it just isn't explicitly declared
// and the properties are Read Only
if (anonymousList.Any())
    Console.WriteLine(anonymousList.First().whateverYouWantToCallIt);


You could use Entity Framework to extract an object model from your database. Afterwards you could add the property for grade to your object (due to the fact that these objects are created in partial classes). This provides a (vastly) more structured / easy to use way of adding custom logic and attributes to your data structure.

You can bind your GUI components to entity framework objects in a similar way as you would using conventional ADO.NET.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜