{"id":7113,"date":"2005-07-08T16:45:00","date_gmt":"2005-07-08T16:45:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/vbteam\/2005\/07\/08\/my-first-database-by-greg-osefo\/"},"modified":"2024-07-05T14:55:08","modified_gmt":"2024-07-05T21:55:08","slug":"my-first-database-by-greg-osefo","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/vbteam\/my-first-database-by-greg-osefo\/","title":{"rendered":"My First Database by Greg Osefo"},"content":{"rendered":"<p><font face=\"Arial\" size=\"2\">&nbsp;&nbsp;&nbsp;Creating my first database was a very interesting experience because I really didn&#8217;t know where to begin.&nbsp; In a past internship I used Visual Studio for some web applications and managing data, but never had to actually create my own database.&nbsp; This summer as an MS Explorer I added an extension to a BlackJack Starter Kit App.&nbsp; My extension utilized a database to store game high scores and that was &#8216;My First Database&#8217;.&nbsp; To begin I added a new database to my project, and along with&nbsp;that a dataset was created.&nbsp; After clicking around in the Server Explorer, I was able to&nbsp;Add a new&nbsp;Table that contained the players name, rank and score.&nbsp; From there I couldn&#8217;t really figure out how to actually get my table to be used inside of my program until I pulled up the Data Souce view.&nbsp; In there I was able to add my new table as the data source for the dataset and a datatable adapter was created.&nbsp; The adapter&nbsp;was my link to the database.&nbsp; <\/font><\/p>\n<p><font face=\"Arial\" size=\"2\">&nbsp;&nbsp;&nbsp;After hooking everything up,&nbsp;it was just a matter of loading and storing data to the database.&nbsp; I already had a listview for displaying highscores, and the code behind the listview would&nbsp;adjust the list&nbsp;when new high scores were made.&nbsp; My plan was to use this list and&nbsp;actually populate&nbsp;it on&nbsp;form load and save&nbsp;it on form close.&nbsp; Populating the list wasn&#8217;t that bad, I just had to initially set up the database to have a couple rows with some default and on form load each listview item\/subitem would be set to the value of&nbsp;the cell in the database.&nbsp; I figured saving wouldn&#8217;t be any more difficult, but I was wrong.&nbsp; I actually went through and changed each value within the database, but when I did my database update VS would produce an error.&nbsp; At the time I didn&#8217;t really understand why this code didn&#8217;t work, and I actually contemplated scrapping the whole database idea all together.<\/font><\/p>\n<p><font face=\"Arial\" size=\"2\">&nbsp;&nbsp;&nbsp;After talking to a couple of people (in particular Steve Lasker), I realized that my problem was that I didn&#8217;t have a primary key.&nbsp; A primary key is basically a unique identifier for database data.&nbsp; Steve&nbsp;explained to me that my problem was that there was no Update method.&nbsp; You may be wondering, why wasn&#8217;t the update function generated automatically when the datatable adapter was created.&nbsp; And the answer is, when a primary key isn&#8217;t assigned, the the datatable adapter doesn&#8217;t know specifically which row to update and therefore in order to prevent data loss does not generate this function.&nbsp; So in order to get an Update method I had to assign a primary key, delete my table from the dataset and re-drag it into the dataset view in order to have a generated Update method and then my program worked perfectly.<\/font><\/p>\n<p><font face=\"Arial\" size=\"2\">&nbsp;&nbsp;&nbsp;So after creating &#8216;My First Database&#8217;, I learned the importance of assigning a primary key and I also learned a lot of cool drag and drop features for databases in VS2005.<\/font><\/p>\n<p><font face=\"Arial\" size=\"2\">Links:<\/font><\/p>\n<p><font face=\"Arial\" size=\"2\">Primary Key Definition: <\/font><a href=\"http:\/\/databases.about.com\/cs\/administration\/g\/primarykey.htm\"><font face=\"Arial\" size=\"2\">http:\/\/databases.about.com\/cs\/administration\/g\/primarykey.htm<\/font><\/a><font color=\"#0000ff\"><\/font><\/p>\n<p><font color=\"#000000\"><font face=\"Arial\" size=\"2\">SQL Database Glossary: <\/font><a href=\"http:\/\/www.sqlstrings.com\/Database-Glossary.htm\"><font face=\"Arial\" size=\"2\">http:\/\/www.sqlstrings.com\/Database-Glossary.htm<\/font><\/a><\/font><\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp;&nbsp;&nbsp;Creating my first database was a very interesting experience because I really didn&#8217;t know where to begin.&nbsp; In a past internship I used Visual Studio for some web applications and managing data, but never had to actually create my own database.&nbsp; This summer as an MS Explorer I added an extension to a BlackJack Starter [&hellip;]<\/p>\n","protected":false},"author":260,"featured_media":8818,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[192,195],"tags":[],"class_list":["post-7113","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-featured","category-visual-basic"],"acf":[],"blog_post_summary":"<p>&nbsp;&nbsp;&nbsp;Creating my first database was a very interesting experience because I really didn&#8217;t know where to begin.&nbsp; In a past internship I used Visual Studio for some web applications and managing data, but never had to actually create my own database.&nbsp; This summer as an MS Explorer I added an extension to a BlackJack Starter [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/posts\/7113","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/users\/260"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/comments?post=7113"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/posts\/7113\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/media\/8818"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/media?parent=7113"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/categories?post=7113"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/tags?post=7113"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}