开发者

Database of built-in data linked with application

I'm coding an application in C#, and I need like, a database of built in data that's linked with the application. I wont ever need to edit the data, I 开发者_如何学运维just need easy access to it.

Basically I want it so I have data built into the application that's represented like this:

Book Title            Book Author
SomeBookTitle         SomeBookAuthor
SomeOtherBookTitle    SomeOtherBookAuthor

Kinda like that? Easy accessible, and preferably linked with the application somehow, so I only have 1 executable file. Oh and there might be a lot of data...


I suggest a different approach than the others so far.

I wouldn't use a resource file because presumably you don't want to hard code every reference to every line in the resource file. 100 "books" would mean 100 distinct resources to code against.

And if you want self contained deployment, 3rd party components are problematic.

My suggestion: Put all your data in an XML structure and put that XML in a resource file as one single resource.

<?xml version="1.0" standalone="yes"?>
<books>
    <book Author="Joe" Title="Joe's Awesome"/>
    <book Author="Fred" Title="Joe's Stupid"/>
    <book Author="Tim" Title="Who's Joe"/>
</books>

Put that in your resource file as MyData

Then you can iterate it with:

XDocument xd = new XDocument(XDocument.Parse(myResources.MyData));
foreach(XElement xe in xd.Elements) { // blah blah blah }

Or you can use LinqToXml or any number of alternatives.


If you only have two fields you could use a Resource File and use the key as your bookname and the value as the author. http://msdn.microsoft.com/en-us/library/7k989cfy(v=vs.80).aspx

You could also add a Sql Server compact database http://msdn.microsoft.com/en-us/library/gg606540.aspx


As far as your database needs are concerned, an embedded database like SQLite or SQL Server CE should do the trick. Kinda like maybe... ;)


You can write your code something like this:

SqlDataReader rdr = null;
SqlConnection conn = new SqlConnection( /*Connection string*/ );

SqlCommand cmd  = new SqlCommand("select * from  Books", conn);
conn.Open();
rdr = cmd.ExecuteReader();
while (rdr.Read())
{
   string bookTitle= (string)rdr["Book Title"];
   string bookAuthor = (string)rdr["Book Author"];

   Console.Write("{0,-25}", bookTitle);
   Console.Write("{0,-20}", bookAuthor );

   Console.WriteLine();
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜