C# .NET - How to add SQL SERVER Database to a Windows Desktop project ?

Asked By Jipin M
20-Oct-11 06:22 AM
Hi all.........

I wish to develop a Windows Application.
How to compact  it with SQL Server database ..(Steps for adding database)
And How to connect with the database? (Code for connectivity)......

I use VS 2008 (SQL Server 2005)

Regards...............
  smr replied to Jipin M
20-Oct-11 06:29 AM

hi

For ASP.NET 2.0 applications, you should store connection strings in the <connectionStrings> section of your application's Web.config file. The connection string used with Windows authentication must include either the Trusted_Connection=Yes attribute, or the equivalent attribute Integrated Security=SSPI, as shown here.
  
<connectionStrings>
  <add name="MyDbConn1" 
     connectionString="Server=MyServer;Database=MyDb;Trusted_Connection=Yes;"/>
  <add name="MyDbConn2" 
    connectionString="Initial Catalog=MyDb;Data Source=MyServer;Integrated Security=SSPI;"/>
</connectionStrings> 

  The above two strings are equivalent and both result in Windows authentication to the database.
  
If you are using data binding expressions in your Web pages, use the ConnectionStrings: qualifier in an ASP.NET expression to use a connection string stored in your application's Web.config file as shown here.
  
<asp:SqlDataSource ID="SqlDataSource1" Runat="server" 
  ConnectionString="<%$ ConnectionStrings:MyDbConn1%>">
  ...
</asp:SqlDataSource>
  To access a connection string in code, use ConfigurationManager.ConnectionStrings as shown here.
  
string connStr = ConfigurationManager.ConnectionStrings["MyDbConn1"].ToString();
SqlConnection conn = new SqlConnection(connStr);



refer
http://msdn.microsoft.com/en-us/library/ff647396.aspx
http://www.asp.net/mvc/tutorials/authenticating-users-with-forms-authentication-cs

  Riley K replied to Jipin M
20-Oct-11 06:46 AM


First I suggest you to learn ADO.Net

You connect to database using SQLCONNECTION

The sequence of operations occurring in the lifetime of a SqlConnection are as follows:
  1. Instantiate the SqlConnection.
  2. Open the connection.
  3. Pass the connection to other ADO.NET objects.
  4. Perform database operations with the other ADO.NET objects.
  5. Close the connection.
Use these namespaces

using System;
using System.Data;
using System.Data.SqlClient;


Here is the example

static void Main()
  {
    // 1. Instantiate the connection
    SqlConnection conn = new SqlConnection(
      "Data Source=(local);Initial Catalog=Northwind;Integrated Security=SSPI");
 
    SqlDataReader rdr = null;
 
    try
    {
      // 2. Open the connection
      conn.Open();
 
      // 3. Pass the connection to a command object
      SqlCommand cmd = new SqlCommand("select * from Customers", conn);
 
      //
      // 4. Use the connection
      //
 
      // get query results
      rdr = cmd.ExecuteReader();
 
      // print the CustomerID of each record
      while (rdr.Read())
      {
        Console.WriteLine(rdr[0]);
      }
    }
    finally
    {
      // close the reader
      if (rdr != null)
      {
        rdr.Close();
      }
 
      // 5. Close the connection
      if (conn != null)
      {
        conn.Close();
      }
    }
  }


Regards
  sam sam replied to Jipin M
23-Oct-11 09:58 AM

You should be able to choose the SQL Server Database file option to get the right kind of database (thesystem.data.SqlClient provider), and then manually correct the connection string to point to your db.

I think the reasoning behind those db choices probably goes something like this:

  • If you're using the Express Edition, and you're not using Visual Web Developer, you're probably building a desktop program.
  • If you're building a desktop program, and you're using the express edition, you're probably a hobbyist or uISV-er working at home rather than doing development for a corporation.
  • If you're not developing for a corporation, your app is probably destined for the end-user and your data store is probably going on their local machine.
  • You really shouldn't be deploying server-class databases to end-user desktops.

However, this logic doesn't quite hold. Even if each of those 4 points is true 90% of the time, by the time you apply all four of them it only applies to ~65% of your audience, which means up to 35% of the express market might want to talk to a server-class db, and that's a significant group. And so, the simplified (greedy) version:

  • If you can get access to a real db server (and the hardware to run it), you ought to be able to afford at least the standard edition of visual studio.
Create New Account
help
Cannot open server 2000 file in SQL SERVER 2005 SQL Server I installed SQL SERVER 2005 replacing SQL SERVER 2000 and now I cannot open the 2000 database file. Do
replication sql 2000 - -> sql 2005 SQL Server , sql, 2005" / > Is it possible to replicate a db from sql server 2000 to sql server 2005? When I set the publications, I look this "select
SSIS for SQL Server 2005? SQL Server How do I download SSIS for SQL Server 2005? SQL Server Programming Discussions SQL Server 2005 (1) Distributed (1) Imp (1) Exp (1) E5C6F1F60688
DTS in SQL Server 2005 SQL Server With the SQL server 2005, how can I invoke DTS? Should I install 'SQL Server Business Intelligence Development Studio'? If
Where is my SQL Server 2005 ? SQL Server I installed SQL Server 2005 on my machine, but when I go to the SQL Server Management Studio and connect