I want easy code to linke the sql server 2008 with c#
I want any one please to explain what prameter the function need ,
I tried this but it doesnot work
Dim connect As New SqlConnection( _
"Server=mycomp;UID=jeff;password=yeah;database=Dummies")
connect.Open()
Loading
Sam HobbsPosted Apr 24, 2011, 1:51 PM
Ankit NandekarPosted Apr 24, 2011, 4:34 AM
Ado.net Provides various Class which can be used for datasource Communication under the following namespaces:
System.Data
System.Data.SqlClient
System.Data.OleDb
System.Data.Odbc
System.Data: collections of type use to holding and managing data on client machine.
System.Data.OleDb: collections of type use to communicate with any Datasource.
System.Data.SqlClient: collections of type use to Communicate only with SqlServer
System.Data.Odbc: collections of type use to communicate with traditional odbc drivers.
above namspaces contain set of class as following
Connection,Command,DataReader,DataAdapter,CommandBuilder etc.
Note :Each Class Reffered By Prefexing With Namwspace Name Like:
oledbConnection,SqlConnection etc
this is the Sample Example in this example we r fetching Data from Database and showing into Combobox.
Design Form Take one Combobox with name cmbcolumn write following code.
Imports System.Data.SqlClient;
Public Class Form1
Dim cn As SqlConnection
Dim cmd As SqlCommand
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
cn=New SqlConnection("Server=[Servername where ur databse store];database=[Databasename];uid=sa;pwd=[password]");
cmd = New SqlCommand()
cmd.Connection = cn
cmd.CommandText = "Select [columnname] from [Tablename]"
cn.Open()
'perform any operation which u want with data like
Dim dr As SqlDataReader = cmd.ExecuteReader()
While dr.Read()
cmbcolumn.Items.Add(dr(0)) 'here cmbcolumn is a comboBox
End While
cn.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
End Class
If this information is useful for u plz mark as accepted answer
Soft CornerPosted Apr 24, 2011, 12:12 AM
The code which you have used is in VB.Net, for connecting sql server thro' C# , Code:
1. Add reference using System.data.sqlclient
2. Create Connection object
SqlConnection conn = new SqlConnection(
"Data Source=DatabaseServer;Initial Catalog=Northwind;User ID=YourUserID;Password=YourPassword");
3. Open & Close Connection
using above created connection object, you can open & close the connection :
conn.Open();
//do something
conn.Close();
Check with attachment for sample code.