views:

546

answers:

7

I am trying to write a local program management and install system for my home network, and i think i've got the technologies nailed down:

  • C#/.NET/WPF for the client
  • Lua for installation scripting support (through LuaInterface)
  • SQL Server Express for maintaining a database of programs

However i'm unsure what specifically i'll use to connect C# to the database. Is there something built into the .NET framework for this? Bonus points if you have a suggestion on what i should use for interacting with said database.

A: 

Sure of course, you can just use the classes in System.Data.SqlClient, though most people will use an ORM. I use LLBLGen Pro.

Noon Silk
+10  A: 

SqlConnection

object is made for this.

Eg:

SqlConnection conn = new SqlConnection(
    "Data Source=(local);Initial Catalog=Northwind;Integrated Security=SSPI");

or

SqlConnection conn = new SqlConnection(
"Data Source=DatabaseServer; Initial Catalog=Northwind; User ID=YourUserID; Password=YourPassword");

conn.Open(); // opens the database connection

Edit:

After doing all your stuff you have to close the connection by

conn.Close();

Data Source: Identifies the server. Could be local machine, machine domain name, or IP Address.

Initial Catalog: Database name.

Integrated Security: Set to SSPI to make connection with user's Windows login

User ID: Name of user configured in SQL Server.

Password: Password matching SQL Server User ID.

rahul
Is there any specific reason for the down vote?
rahul
+1 and a bit curious about the downvote to.
Lieven
+1 from a fellow "downvote with no comment" recipient. There is nothing incorrect or unhelpful in this answer.
Christian Hayter
[Disclaimer: i didn't downvote it] How do i use an Entity Framework or Linq to SQL with this?
RCIX
It was me. And it's because the example doesn't cover closing the connection :)
Noon Silk
Thanks @silky. Edited my post with connection closing also. Thanks for pointing this out.
rahul
lol. A bit of a stretch but now someone should come in and downvote for just opening and closing the connection without actually doing something with it. The real reason offcourse should only be divulged after enough people wonder why oh why...
Lieven
+2  A: 

To connect to SQL Server Express you need nothing but System.Data, which is a standard .NET assembly. Just use SqlXXX classes and you'll be done.

However, writing mundane ADO.NET code is very boring, so it's very common to use an ORM or less heavy-weight result-set mapper such as BLToolkit.

And finally, consider using SQL Server CE. This is a fully ACID-compliant single-file embedded database engine which supports pretty much any feature you can expect form an SQL RDBMS.

Anton Gogolev
+6  A: 

Check out

I'm sure there's plenty more out there - just google for "ADO.NET" and "Tutorial" ......

UPDATE:

If you want to connect to your local SQL Server Express, and connect to the "Northwind" database, and read the top 5 customers from the "Customers" table, you'd have to do something like this:

string connectionString = "server=(local)\SQLExpress;database=Northwind;integrated Security=SSPI;";

using(SqlConnection _con = new SqlConnection(connectionString))
{
   string queryStatement = "SELECT TOP 5 * FROM dbo.Customers ORDER BY CustomerID";

   using(SqlCommand _cmd = new SqlCommand(queryStatement, _con))
   {
      DataTable customerTable = new DataTable("Top5Customers");

      SqlDataAdapter _dap = new SqlDataAdapter(_cmd);

      _con.Open();
      _dap.Fill(customerTable);
      _con.Close():

   }
}

Now you would have all 5 top customers from your Northwind database in the DataTable and you can inspect them, print them out, manipulate them - whatever you want to do.

That's ADO.NET in action!

As for the details of the connection string - what options you can use and what it should look like, check out the Connection Strings web site - it has tons of examples and explanations.

Marc

marc_s
Um, this may sound like a dumb question but how doesw ADO.NET relate to my question?
RCIX
ADO.NET is the .NET subsystem to connect from C# or VB.NET to a database like SQL Server, SQL SErver Express, Oracle or whatever
marc_s
Ok, how do i use LINQ to SQL or similar with this?
RCIX
LINQ-to-SQL builds on top of ADO.NET - you don't "waste" anything if you first learn the basics of ADO.NET, and once you understand that, move on to LINQ-to-SQL or Entity Framework.
marc_s
+1  A: 

Currently the easiest way to connect to your database and perform queries in C# is LinqToSQL. It will save you a lot of headache as compared to using "old-school" ADO connections.

Adrian Grigore
+1  A: 

You can use ADO.Net and System.Data.SqlClient namespace for the same. I will advise you to go with Entities framework (ORM). Please find below links for Entity Framework walk through

http://thedatafarm.com/LearnEntityFramework/tutorials/creating-an-ado-net-entity-framework-entity-data-model/

http://thedatafarm.com/LearnEntityFramework/tutorials/use-an-entity-framework-entity-as-a-winforms-data-source/

Mahin
That deals with using an object as a database, i've set up an instance of SQL server express for that.
RCIX
EF seems a bit overkill just for a very small beginner's sample...... unnecessarily complicates things, in my opinion. Learn the basics of bare bones ADO.NET first!
marc_s
I agree with marc_s.its always good to learn basics first and I dont think it will take much time also ( for ADO.Net )
Mahin
+1  A: 

I would recommend using Microsoft's Patterns & Practices Enterprise Library. You would specifically be using the The Data Access Application Block.

An excerpt from MSDN:

The Data Access Application Block provides the following benefits:

  • It uses the functionality provided by ADO.NET 2.0 and with it, you can use ADO.NET functionality along with the application block's functionality.
  • It reduces the need to write boilerplate code to perform standard tasks.
  • It helps maintain consistent data access practices, both within an application and across the enterprise.
  • It reduces difficulties in changing the database type.
  • It relieves developers from learning different programming models for different types of databases.
  • It reduces the amount of code that developers must write when they port applications to different types of databases.

I've used this method for years and it's been very successfull thus far. Good luck!

Mr. Smith