how to connect sql database with vb.net form
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
lich
Junior Poster in Training
79 posts since May 2008
Reputation Points: -1
Solved Threads: 4
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.
bajanpoet
Junior Poster in Training
96 posts since Sep 2006
Reputation Points: 10
Solved Threads: 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
lich
Junior Poster in Training
79 posts since May 2008
Reputation Points: -1
Solved Threads: 4
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..
lich
Junior Poster in Training
79 posts since May 2008
Reputation Points: -1
Solved Threads: 4
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?
bajanpoet
Junior Poster in Training
96 posts since Sep 2006
Reputation Points: 10
Solved Threads: 0