Your first ADO.NET application against SQL Server


Since we have decided to use sqlhelper.cs to wrap our database calls the first application actually gets pretty simple.

First step is connection string. Depending on if you are using SQL Server 2000 or 2005 it might vary. It might also vary depending on the fact that you are using integrated security etc, so in short please look at these links for further details

http://www.connectionstrings.com/?carrier=sqlserver

http://www.connectionstrings.com/?carrier=sqlserver2005

Here's the connection string I am using

Data Source=(local);Network Library=DBMSSOCN;Initial Catalog=Northwind;User ID=sa;Password=whatever;

Now lets assume you will be programming against the Northwind database. The good thingabout  SQL Server from a programmers perspective is that it's built into framework, giving us some positive effects

- No external driver to download or try to get working. The System.Data.SqlClient namespace is where the classes resides

- works in ASP.NET medium trust by default. Cause there could typically be two things messing with you when using an external component in medium trust, first not being allowed to do sockets (network access) at all and second the dreaded partially trusted callers    

However, here we will be creating a Windows Forms app against SQL Server:



            //First step - open/close connection. 
            //No need to the sqlhelper class takes care of that
            string sConn = "Data Source=(local);Network Library=DBMSSOCN;Initial Catalog=Northwind;User ID=sa;Password=stefan;";
            //The sql
            string sSQL = "select * from products";
            //open conn,execute,close conn - and get the first datatable in a single call
            DataTable dt = Microsoft.ApplicationBlocks.Data.SqlHelper.ExecuteDataset(sConn,
                CommandType.Text, sSQL).Tables[0];
            //set as datasource
            dataGridView1.DataSource = dt;