1,105,312 Community Members

how to connect sql database with vb.net form

Member Avatar
lich
Junior Poster in Training
78 posts since May 2008
Reputation Points: -11 [?]
Q&As Helped to Solve: 4 [?]
Skill Endorsements: 0 [?]
 
0
 

i want to connect the sql dabase to vb.net form. can anyone tel me how to do it. i tired google. but it only says about ms access database.

please help me

Member Avatar
freshfitz
Posting Pro in Training
436 posts since Sep 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 36 [?]
Skill Endorsements: 0 [?]
 
0
 

Go into database explorer click connect to database

Member Avatar
bajanpoet
Junior Poster in Training
96 posts since Sep 2006
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

In code you can do it this way

Imports System.Data
Imports System.Data.SqlClient
Public conn As SqlConnection
conn = New SqlConnection("server = SRVERNAME ;database = DATABASENAME;Trusted_Connection = yes")

Then you can use conn.Open to open the connection and conn.close to close it.

Member Avatar
san_gwapo19
Newbie Poster
14 posts since Jul 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Imports System.Data.SqlClient
Dim con As New SqlClient.SqlConnection("data source=DATASOURCE;initial catalog=NAME OF DATABASE;Integrated Security=True")
Dim cmd As New SqlCommand()
cmd.Connection = con
con.Open()
con.Close()

THIS IS WHAT I USE TO CONNECT SQL TO VB.NET
this is a sample code below:

Imports System.Data.SqlClient

Public Class NEW_PERSONAL_INFORMATION

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim con As New SqlClient.SqlConnection("data source=ROOM310-40\SQLEXPRESS;initial catalog=DTIS;Integrated Security=True")
Dim cmd As New SqlCommand()
cmd.Connection = con
con.Open()
cmd.CommandText = "insert into perinfo values ('" & empid.Text & "','" & sname.Text & "','" & fname.Text & "','" & mname.Text & "','" & birthdate.Text & "','" & birthplace.Text & "','" & gender.Text & "','" & cstatus.Text & "','" & citizenship.Text & "','" & height.Text & "','" & weight.Text & "','" & btype.Text & "','" & address.Text & "','" & cp.Text & "','" & zcode.Text & "','" & telno.Text & "','" & email.Text & "','" & fathername.Text & "','" & mothername.Text & "'')"
cmd.ExecuteNonQuery()

MsgBox("Record is successfully stored")
con.Close()
End Sub
End Class

hope this can help

Member Avatar
lich
Junior Poster in Training
78 posts since May 2008
Reputation Points: -11 [?]
Q&As Helped to Solve: 4 [?]
Skill Endorsements: 0 [?]
 
0
 

Thanks a lot

i have did it.. but when i try to debug.. it gives me a error.. can anyone tell me y

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


Dim con As New SqlClient.SqlConnection("data source=D:\Crap\Dialog Application Check Bill\Dialog Bill Check\Dialog Bill Check\database.mdf")
Dim cmd As New SqlCommand()
cmd.Connection = con
con.Open()
cmd.CommandText = "insert into DailySMS values ('" & txtInternational.Text & "','" & txtLocal.Text & "')"
cmd.ExecuteNonQuery()

MsgBox("Record is successfully stored")
con.Close()

End Sub
End Class

Member Avatar
lich
Junior Poster in Training
78 posts since May 2008
Reputation Points: -11 [?]
Q&As Helped to Solve: 4 [?]
Skill Endorsements: 0 [?]
 
0
 

Imports System.Data.SqlClient
Dim con As New SqlClient.SqlConnection("data source=DATASOURCE;initial catalog=NAME OF DATABASE;Integrated Security=True")
Dim cmd As New SqlCommand()
cmd.Connection = con
con.Open()
con.Close()

THIS IS WHAT I USE TO CONNECT SQL TO VB.NET
this is a sample code below:

Imports System.Data.SqlClient

Public Class NEW_PERSONAL_INFORMATION

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim con As New SqlClient.SqlConnection("data source=ROOM310-40\SQLEXPRESS;initial catalog=DTIS;Integrated Security=True")
Dim cmd As New SqlCommand()
cmd.Connection = con
con.Open()
cmd.CommandText = "insert into perinfo values ('" & empid.Text & "','" & sname.Text & "','" & fname.Text & "','" & mname.Text & "','" & birthdate.Text & "','" & birthplace.Text & "','" & gender.Text & "','" & cstatus.Text & "','" & citizenship.Text & "','" & height.Text & "','" & weight.Text & "','" & btype.Text & "','" & address.Text & "','" & cp.Text & "','" & zcode.Text & "','" & telno.Text & "','" & email.Text & "','" & fathername.Text & "','" & mothername.Text & "'')"
cmd.ExecuteNonQuery()

MsgBox("Record is successfully stored")
con.Close()
End Sub
End Class

hope this can help

i have copied paste your code and edited it.. but it asking con is not declared..

Member Avatar
bajanpoet
Junior Poster in Training
96 posts since Sep 2006
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

There are two sets of code in the above reply:

Imports System.Data.SqlClient
Dim con As New SqlClient.SqlConnection("data source=DATASOURCE;initial catalog=NAME OF DATABASE;Integrated Security=True")
Dim cmd As New SqlCommand()
cmd.Connection = con
con.Open()
con.Close()

and

Imports System.Data.SqlClient

Public Class NEW_PERSONAL_INFORMATION

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim con As New SqlClient.SqlConnection("data source=ROOM310-40\SQLEXPRESS;initial catalog=DTIS;Integrated Security=True")
Dim cmd As New SqlCommand()
cmd.Connection = con
con.Open()
cmd.CommandText = "insert into perinfo values ('" & empid.Text & "','" & sname.Text & "','" & fname.Text & "','" & mname.Text & "','" & birthdate.Text & "','" & birthplace.Text & "','" & gender.Text & "','" & cstatus.Text & "','" & citizenship.Text & "','" & height.Text & "','" & weight.Text & "','" & btype.Text & "','" & address.Text & "','" & cp.Text & "','" & zcode.Text & "','" & telno.Text & "','" & email.Text & "','" & fathername.Text & "','" & mothername.Text & "'')"
cmd.ExecuteNonQuery()

MsgBox("Record is successfully stored")
con.Close()
End Sub
End Class

Which one are you speaking of?

Member Avatar
aeonmj
Newbie Poster
1 post since Sep 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

i want to connect the sql dabase to vb.net form. can anyone tel me how to do it. i tired google. but it only says about ms access database.

please help me

hi i need help guys please give mo some codes to connect Vb to database..thank you.....im hoping for positive response...

Member Avatar
richardalgor
Newbie Poster
1 post since May 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

For connection from a visual basic form to database , you need a connection string first like the following

connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password"

you have to fill your database information to this connectionstring.

drag a button control to a form and put the follwoing code in to click event

    Dim connetionString As String
    Dim cnn As SqlConnection
    connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password"
    cnn = New SqlConnection(connetionString)
    Try
        cnn.Open()
        MsgBox("Connection Open ! ")
        cnn.Close()
    Catch ex As Exception
        MsgBox("Can not open connection ! ")
    End Try

If you need more programming information

http://vb.net-informations.com/ado.net-dataproviders/ado.net-dataproviders-tutorial.htm

you will get all necessary help from above link

algor

malay01
Newbie Poster
1 post since Nov 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
Unverified Member
 
0
 
Imports System.Data.SqlClient
Dim con As New SqlClient.SqlConnection("data source=DATASOURCE;initial catalog=NAME OF DATABASE;Integrated Security=True")
Dim cmd As New SqlCommand()  
cmd.Connection = con            <--- Here is an erro - Declaration required for 'cmd'
con.Open()
con.Close()

pls try to solve this
im using visual studio 2010
ms sql 2008

Member Avatar
Icone
Junior Poster in Training
87 posts since Oct 2012
Reputation Points: -1 [?]
Q&As Helped to Solve: 5 [?]
Skill Endorsements: 0 [?]
 
0
 
Dim con as sqlclient.sqlconnection = new sqlclient.sqlconnection(Data source=servername;Initial Catalog=databasename;Integrated Security=True;)
Dim cmd as sqlclient.sqlcommand
cmd = new sqlclient.sqlcommand("insert into tblsample(one_one)values('"  & txtone.text & "')",con)
if con.state = connection.closed then con.open
cmd.executenonquery()
msgbox ("Saved.")
con.close

Dont't forget to import - Imports System.Data.SqlClient at the top.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article