Click here to Skip to main content
15,896,269 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I'd like to know if anyone could help me to create a conection between SQL Server 2008 and Visual Studio C# 2010, is just that i've found some tutorials but im just a beginner, and i'm having some troubles to undestand them, if you could tell me where can i find a tutorial for someone like me i would be very thakfull, or if you could help me, better, i want to know how to start the connection and that kind of things, well, thank you!
Posted
Comments
parmar_punit 11-Jun-11 7:46am    
There is a website which have all the way to connect the database with front-end language..
http://www.connectionstring.com

First Import NameSpace
using System.Data.SqlClient;
And Use Following Code
SqlConnection SqlCon = new SqlConnection
("
server=UrSQLserverName;
database=DatabaseName;
uid=SqlUserID;
pwd=SqlPassword
");


And Then Open Sql Connection

SqlCon.Open()


And Do Ur Sql Operations....
 
Share this answer
 
Comments
Juan-ITSM 10-Jun-11 12:16pm    
thanks for your help, its very usefull for me!
hi first u do one thing drag the sql dataSource from tool kit once its draged to ur form u configure the sqldatasouce and it will navigate to some steps once u completed all the steps u can see connection string in the web config file as follows

In webconfig file follow the following code:
XML
<connectionstrings>
    <add name="EmployeeConnectionString" connectionstring="Data Source=.\SQLEXPRESS;Initial Catalog=dbEmployee;Persist Security Info=True;User ID=a;Password=b">
      providerName="System.Data.SqlClient" />
  </add></connectionstrings>

this is for windows authentication mode and once you find this copy the string in the invented commas as follows

in the class u copy as follows


C#
using System.Data;
using System.Data.Sqlclient
class sample
{
    protected void page_Load(object sender,EventArgs e)
    {
         string str="DataSource =the name of server;Initial Catalog=the name of ur database;Integrated Security=True";
         try
         {
             sqlconnection con=new sqlconnection(str);
             con.open();//here Connection takes place
             string commandText="SQl query"; 
             SqlCommand cmd=new Sqlcommand(commadnText,con);//here the command isintialized 
             cmd.ExecuteNonQuery();
         }
         catch(Exception ex)
         {
             lable1.Text=ex.Message;
         }
         finally 
         {
             con.Close();
         }
    }
}


try this and u can do it
[Edit: Added PRE tag]
 
Share this answer
 
v3
Comments
Juan-ITSM 10-Jun-11 12:15pm    
thank you very much, i've been trying to do this, and i've had a litle troubles, even i still have, but things like this help me so much, thank you!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace prjConnection
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
sqlCon.StateChange +=new StateChangeEventHandler(sqlCon_StateChange);
sqlCon.InfoMessage +=new SqlInfoMessageEventHandler(sqlCon_InfoMessage);
}

private SqlConnection sqlCon = new SqlConnection("Data Source = ; Initial Catalog = ; Intergrated Security = True ");



# region Exit
private void btnExit_Click(object sender, EventArgs e)
{
Close();
}
# endregion
# region open
private void btnOpen_Click(object sender, EventArgs e)
{
sqlCon.Open();
}
# endregion
# region close
private void btnClose_Click(object sender, EventArgs e)
{
sqlCon.Close();
}
# endregion
# region state
private void btnState_Click(object sender, EventArgs e)
{
txtState.Text = sqlCon.State.ToString();
}
# endregion
# region connectionstring
private void btnconnection_Click(object sender, EventArgs e)
{
txtConnectionString.Text = sqlCon.ConnectionString;
}
# endregion
# region packet
private void btnPacket_Click(object sender, EventArgs e)
{
txtPacketSize.Text = sqlCon.PacketSize.ToString();
}
# endregion
# region Display
private void btnDisplay_Click(object sender, EventArgs e)
{
txtConnectionString.Text = sqlCon.ConnectionString;
txtDataSource.Text = sqlCon.DataSource;
txtDatabase.Text = sqlCon.Database;
txtPacketSize.Text = sqlCon.PacketSize.ToString();
txtState.Text = sqlCon.State.ToString();
txtConnectionTimeOut.Text = sqlCon.ConnectionTimeout.ToString();
txtWorkID.Text = sqlCon.WorkstationId;

}
# endregion
# region state change
private void sqlcon_StateChange(object sender, StateChangeEventArgs e)
{

MessageBox.Show("The state of the connection is: " + e.CurrentState.ToString() + "The original state is: " + e.OriginalState.ToString());

}
# endregion

private void sqlCon_InfoMessage(object sender, SqlInfoMessageEventArgs e)

{

MessageBox.Show(e.Message);

}

private void btnGetInfo_Click(object sender, EventArgs e)
{
sqlCon.FireInfoMessageEventOnUserErrors = true;
sqlCon.ChangeDatabase("");
}


}
}

just a lil sample of a connection to SQL in C# if you need more help drop an email dragonsraveolution@thepub.co.za
 
Share this answer
 
There is a site dedicated for that. :)
http://www.connectionstrings.com/sql-server-2008[^]
Edit:
And a tutorial
Tutorials: AdoDotNet[^]
 
Share this answer
 
v2
Comments
Kim Togo 12-May-11 5:00am    
Good link! My 5.
yesotaso 10-Jun-11 10:36am    
Thanks.
Juan-ITSM 18-May-11 19:49pm    
Thank you very much, it has been very helpfull for me, thank you again...
yesotaso 10-Jun-11 10:37am    
No problem at all.
In webconfig file follow the following code
    <connectionstrings>
   
    <add name="EmployeeConnectionString" connectionstring="Data Source=.\SQLEXPRESS;Initial Catalog=dbEmployee;Persist Security Info=True;User ID=a;Password=b">
providerName="System.Data.SqlClient" />
  </add></connectionstrings>
 
Share this answer
 
v3
Comments
Juan-ITSM 10-Jun-11 12:18pm    
thank you, it will help me so much...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900