NoCatharsis
NoCatharsis

Reputation: 158

How do I create a simple Windows form to access a SQL Server database?

I believe this is a very novice question, and if I'm using the wrong forum to ask, please advise.

I have a basic understanding of databasing with MS SQL Server, and programming with C++ and C#. I'm trying to teach myself more by setting up my own database with MS SQL Server Express 2008 R2 and accessing it via Windows forms created in C# Express 2010. At this point, I just want to keep it to free or Express dev tools (not necessarily Microsoft though).

Anyway, I created a database using the instructions provided here and I set the data types appropriately for each column (no errors in setup at least).

Now I'm designing the GUI in C# Express but I've kind of hit a wall as far as the database connection.

Is there a simple way to access the database I created locally using C# Express? Can anyone suggest a guide that has all this spelled out already?

I am a self-learner so I look forward to teaching myself how to use these applications, but any pointers to start me off in the right direction would be greatly appreciated.

Upvotes: 6

Views: 25851

Answers (4)

Raj Kamuni
Raj Kamuni

Reputation: 1

its simple

use following code

SqlConnection con = 
 new SqlConnection(@"server=.\SQLEXPRESS;AttachDbFilename=path of database file;Integrated Security=True;User Instance=True");

Upvotes: 0

Abe Miessler
Abe Miessler

Reputation: 85056

I'd start by taking a look at this tutorial:

http://msdn.microsoft.com/en-us/library/ms178371.aspx

The basic ideas is to create a connection string that will define a number of things, including: where your db server is, what the database name is, and how you are connecting to it.

Once you have your connection string defined you will probably use the SqlConnection, SqlCommand and SqlDataReader classes for all interactions with the database. If you click on the links for those three classes and look at the documentation you will see they have pretty good examples.

Upvotes: 4

Mike Jones
Mike Jones

Reputation: 1006

Far and away the easiest way to do this is with LINQ in Visual Studio

You might want to check out this tutorial which will guide you through all the steps you need.

http://www.codegod.de/WebAppCodeGod/tutorial-linq-to-sql---part-1-AID466.aspx

Upvotes: 2

Code Silverback
Code Silverback

Reputation: 3224

You probably want to use old school ADO.NET if you are more comfortable in SQL than C#.

MSDN intro to ADO.NET

Upvotes: 2

Related Questions