• We just launched and are currently in beta. Join us as we build and grow the community.

How to Create a Connection Between C# and SQL Server

TheReaper989

Multithreading Wizard
T Rep
0
0
0
Rep
0
T Vouches
0
0
0
Vouches
0
Posts
123
Likes
54
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
In this tutorial, I will teach you how to create a connection between C#.net and SQL Server 2005 Express Edition. In this method, you can access the database that you have created in the SQL Server 2005 Express Edition wherein, you can select the tables and retrieve the data of it.break

In here, we will use Microsoft Visual Basic Studio 2008 for creating this project.

Let's get started:

Create a databaase and name it "dbtest".

Now, open Microsoft Visual Studio 2008 and create a new project.

connectionsqlcsharpfig.1.jpg


After that, a “New Project” window will appear. Then, select “Visual C#” and create a new Windows Form Application and hit ok.

connectionsqlcsharpfig.2.png


After creating a new Windows Form Application, do the Form just like this.

connectionsqlcsharpfig.3.png


Double click the "Connect Me" button and do the following codes to establish the connection between SQL Server 2005 and C#.Net.

Note: Put using System.Data.SqlClient; above the namespace to access sql server library and to avoid any errors.

  1. private

    void

    button1_Click(

    object

    sender, EventArgs e)
  2. {
  3. //initialize sql connection
  4. SqlConnection con =

    new

    SqlConnection(

    )

    ;
  5. //set a connection string
  6. con.

    ConnectionString

    =

    "Data Source=.\\

    SQLEXPRESS;Database=dbtest;trusted_connection=true;"

    ;

  7. //opening connection
  8. con.

    Open

    (

    )

    ;
  9. //validating connection

  10. if

    (

    con.

    State

    ==

    ConnectionState.

    Open

    )
  11. {
  12. if

    (

    button1.

    Text

    ==

    "Connect Me"

    )
  13. {
  14. button1.

    Text

    =

    "Disconnect Me"

    ;
  15. label2.

    Text

    =

    "Connected"

    ;
  16. }
  17. else
  18. {
  19. button1.

    Text

    =

    "Connect Me"

    ;
  20. label2.

    Text

    =

    "Disconnected"

    ;
  21. con.

    Close

    (

    )

    ;
  22. }

  23. }

  24. }

Output :

connectionsqlcsharpfig.4.png


Here is another example that you can also perform.

  1. private

    void

    button1_Click(

    object

    sender, EventArgs e)
  2. {
  3. //initialize sql connection
  4. SqlConnection con =

    new

    SqlConnection(

    )

    ;
  5. //set a connection string
  6. con.

    ConnectionString

    =

    "Server=.\\

    SQLEXPRESS;"

    +
  7. "User Instance=true;"

    +
  8. "Integrated Security=true;"

    +
  9. "AttachDbFilename="

    +

    Application.

    StartupPath

    +

    "\\

    dbtest.mdf;"

    ;




  10. //opening connection
  11. con.

    Open

    (

    )

    ;
  12. //validating connection

  13. if

    (

    con.

    State

    ==

    ConnectionState.

    Open

    )
  14. {
  15. if

    (

    button1.

    Text

    ==

    "Connect Me"

    )
  16. {
  17. button1.

    Text

    =

    "Disconnect Me"

    ;
  18. label2.

    Text

    =

    "Connected"

    ;
  19. }
  20. else
  21. {
  22. button1.

    Text

    =

    "Connect Me"

    ;
  23. label2.

    Text

    =

    "Disconnected"

    ;
  24. con.

    Close

    (

    )

    ;
  25. }

  26. }

  27. }

Reminder:To make the second option work, you are required to put the database file(dbtest.mdf) inside the bin folder of your project.

Output :

connectionsqlcsharpfig.4.png


For all students who need programmer for your thesis system or anyone who needs a sourcecode in any programming languages. You can contact me @ :

Email – [email protected]
Mobile No. – 09305235027 – tnt

 

452,292

323,341

323,350

Top