I have a Visual C# Console Application (Eventually I will add some graphics). I would like to integrate this project with a database with the following functionality:
- During execution the program shall be able to write data to the database
- During execution the program shall be able to read data from the database
- The database shall be stored on a server (Locally will do for now)
How can I obtain this functionality in Visual Studio 2010 Ultimate?
(If you need more information let me know)
What “item” should I add to my project in Visual Studio so that I can write SQL statements with query strings?
Over time I would like to be able to store thousands of records to an online database.
Thanks!
I’m not going to write actual code for you, because I don’t see any indication you’ve tried to do this yourself first. Here are the basic steps involved, though:
Add references to
System.Data,System.Data.Common, andSystem.Data.SqlClientto your project.Declare member (class) variables to hold references to an
SqlAdapter,SqlConnection, andSqlCommand.Create each of the above in your class constructor or initialization method, setting the properties as needed for each (for instance,
SqlConnectionrequires that you provide a value forConnectionString, andSqlCommandneedCommandTypeandCommandTextvalues, and so forth).Use the above member variables to access data just like you would in a GUI app, except you have no UI controls to use to display the data – all data access is via your code.
For specific ways to set things up, create a standard WinForms application and set up your database connection, query, and so forth. Look at the variables that VS creates to hold each of them, and how they’re initialized in
InitializeComponents– you need to perform those same steps yourself in your code instead of via dragging and dropping things in the VS user interface.This should be enough to get you started. Once you’ve tried, you can post any questions related to specific issues as new questions here on SO.