473,473 Members | 1,831 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to Connect to Sql Server 2008 and Query using C# ??

16 New Member
Code for C# that easy to understand...
Sep 7 '11 #1
2 9487
snehasismishra1
18 New Member
Hi,

From your question i understood, you want code for data base connection. For that i have posted the code.

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Data;
  3. using System.Data.SqlClient;
  4.  
  5. class SqlConnectionDemo
  6. {
  7.     static void Main()
  8.     {
  9.         // 1. Instantiate the connection
  10.         SqlConnection conn = new SqlConnection("Data Source=(local);Initial Catalog=Northwind;Integrated Security=SSPI");
  11.         SqlDataReader rdr = null;
  12.         try
  13.         {
  14.             // 2. Open the connection
  15.             conn.Open();
  16.             // 3. Pass the connection to a command object
  17.             SqlCommand cmd = new SqlCommand("select * from Customers", conn);
  18.             // 4. Use the connection
  19.             // get query results
  20.             rdr = cmd.ExecuteReader();
  21.  
  22.             // print the CustomerID of each record
  23.             while (rdr.Read())
  24.             {
  25.                 Console.WriteLine(rdr[0]);
  26.             }
  27.         }
  28.         finally
  29.         {
  30.             // close the reader
  31.             if (rdr != null)
  32.             {
  33.                 rdr.Close();
  34.             }
  35.  
  36.             // 5. Close the connection
  37.             if (conn != null)
  38.             {
  39.                 conn.Close();
  40.             }
  41.         }
  42.     }
  43. }
If you want something different, please elaborate.

Thanks,
Snehasis
Sep 7 '11 #2
LeLouch Fenette
16 New Member
Nice one dude...
I try to transfer into function.
Sep 7 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Jerome | last post by:
Hi, I've got the following problem and I don't know how to make this (if it's possible at all): I want to make a form where the user can enter a criteria and then, after pushing a button or...
2
by: jeffhan | last post by:
we have os/400 db2 database at the backend. i installed db2 v9 connect server on one windows server which is locating the same network with database server. now how to i configure the connect...
1
by: GatorDBA | last post by:
Hey Folks, Experiencing strange problem with SQL 2005 running on Windows Server 2008. When trying to connect via named pipes or tcp, it takes 25-45 seconds for the connection to be established....
1
by: =?Utf-8?B?bWFzYWtpeQ==?= | last post by:
Hello, I'm trying to develop 32-bit Windows Native C/C++ application using Visual Studio 2008, on 64-bit Windows Server 2008. Q1. Is it possible to develop 32-bit C/C++ application using VS 2008...
1
by: Ken Fine | last post by:
I have set up Microsoft Search Server 2008 Express. I want to know how I can query against it and return results in a form that can be bound to ASP.NET controls like ListViews. If there's simply...
5
by: nmailey | last post by:
Hi all. I've been working in SQL 2000 for a while and have just installed SQL Server 2008 (the Free Edition (express I guess)) on my machine. I've got MS Visual Web Developer Express which I've...
2
by: =?Utf-8?B?SmVycnkgQw==?= | last post by:
I have a server 2008 IIS 7.0 with indexing service installed. I have created the catalog and have a test page using these posts:...
0
by: LitaOsiris | last post by:
Hi everyone, I'm doing a website in JSP. I'm using Tomcat 6 and have gotten the site talking to the backend file. However, when I go to add in a database connection, I have problems. This is my...
4
by: nagmvs | last post by:
Hi i installed Sql Server 2008 R2 in my windows 7 system.When i open the sql server 2008 R2 Management studio and try to connect using the options like Server Type : Database Engine Server Name :...
8
by: Mangala Amargol | last post by:
For example,runnig "ipconfig" command through perl script and printing it on web browser. my $cmd = "ipconfig"; my $output = `$cmd`; print $output; Above code works fine in Windows Server...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.