-3

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:

  1. During execution the program shall be able to write data to the database
  2. During execution the program shall be able to read data from the database
  3. 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!

10
  • 5
    What have you tried? This is so simple to do, you are sure to pick up down votes. There are plenty of walkthroughs for this. Commented Jul 19, 2012 at 1:40
  • I just do not know what the best 'item' to add in Visual Studio is. Commented Jul 19, 2012 at 1:45
  • 1
    There's absolutely no difference in connecting to and using a database between WinForm and console applications; you just do it without the graphical user interface. See this answer to a very similar question (found in the Related list to the right of your question, which means you should have found it even without a search, BTW). Commented Jul 19, 2012 at 1:47
  • You don't add anything visually in VS. You create everything yourself in code (declaring variables in your class to hold them). The whole point of a console app is you don't have a GUI, which means you have nothing to drop "items" on. Commented Jul 19, 2012 at 1:54
  • @GrayFox374 If this is such a simple question, answer it. Maybe I am not conveying my question accurately. Commented Jul 19, 2012 at 1:58

1 Answer 1

12

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:

  1. Add references to System.Data, System.Data.Common, and System.Data.SqlClient to your project.

  2. Declare member (class) variables to hold references to an SqlAdapter, SqlConnection, and SqlCommand.

  3. Create each of the above in your class constructor or initialization method, setting the properties as needed for each (for instance, SqlConnection requires that you provide a value for ConnectionString, and SqlCommand need CommandType and CommandText values, and so forth).

  4. 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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.