ads_header

How To Create Connection With SQL Server In Visual Studio


How To Create Connection With SQL Server In Visual Studio


Hello friends, in this post maybe a lot to discuss, but I will show again about how to connect project in Visual Studio 2010 with Microsoft SQL Server 2008. This connection process is almost similar to connecting to other databases such as MySQL. SQL Server is a very complex DBMS, because there are many features in it that some other DBMS may not have.

In this tutorial I only use one button to ensure that our project has been successfully connected to the database that we created in SQL Server.

Previously, I've created a database with the name "tes" in SQL Server 2008


now, open Visual Studio and create a new project, then add one Button in the Form. Then, create a module and enter the following code to connect to the "tes" database

Imports System.Data.SqlClient
Module connect
   Public conn As SqlConnection

   Sub koneksinya()
     Dim str As String = 
     conn = new SqlConnection(str)
     If conn.State = ConnectionState.Closed Then
        conn.Open()
        MsgBox("Connection Successfully")
     End If
   End Sub

End Module





We need to fill the str variable with the database attribute in SQL Server 2008. Then, click Menu Tools and then select Connect to Database


then, fill in according to the database name and name of your pc, then click OK



then, in the properties section, copy the contents of the Connection String


Paste into variable str

Imports System.Data.SqlClient
Module connect
   Public conn As SqlConnection

   Sub koneksinya()
     Dim str As String = "Data Source=ndiappink-pc\sqlexpress; Initial Catalog=tes;Integrated Security=True"  
     conn = new SqlConnection(str)
     If conn.State = ConnectionState.Closed Then
        conn.Open()
        MsgBox("Connection Successfully")
     End If
   End Sub

End Module

When done, call Sub koneksinya in Button 1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click  
    Call koneksinya()
End Sub

And finally, the program is ready to run.

For more details, please watch the video below


No comments:

Powered by Blogger.