| | |
Sql server connection using code in VB.NET
Please support our VB.NET advertiser: Intel Parallel Studio Home
![]() |
There is just two ways to live your life.
One is as though nothing is a miracle.
The other is as if everything is.
One is as though nothing is a miracle.
The other is as if everything is.
•
•
Join Date: Jul 2007
Posts: 1
Reputation:
Solved Threads: 0
•
•
•
•
Hi,
Does anyone have some good examples of ADO.NET code that demonstrates a coded connection to SQL Server such that bound controls bind to retrieve and update data.
Thanks
Dim SQLConn As SqlConnection = New SqlConnection
SQLConn.ConnectionString = "Data Source=servername;" & _
"Initial Catalog=databasename;" & _
"User ID=username;" & _
"Password=userpassword;"
•
•
Join Date: Aug 2007
Posts: 2
Reputation:
Solved Threads: 0
the easy and best way to get connected with sql server is given below:-
"declare this in the code window on a form. this should be first line in the the code window, import the library class."
import system.data.sqlclient
"then declare these publically"
dim con as sqlconnection
dim cmd as sqlcommand
dim rs as recordset
dim da as sqldataadaptor
"on form load make the connection:"
con= new sqlconnection("connection string")
con.open
msgbox"ok"
"you are connected with sql server"
still you have any query pls mention.
"declare this in the code window on a form. this should be first line in the the code window, import the library class."
import system.data.sqlclient
"then declare these publically"
dim con as sqlconnection
dim cmd as sqlcommand
dim rs as recordset
dim da as sqldataadaptor
"on form load make the connection:"
con= new sqlconnection("connection string")
con.open
msgbox"ok"
"you are connected with sql server"
still you have any query pls mention.
•
•
Join Date: Jun 2008
Posts: 23
Reputation:
Solved Threads: 0
connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password"
connection = New SqlConnection(connetionString)
http://vb.net-informations.com/dataa...-sqlserver.htm
connection = New SqlConnection(connetionString)
http://vb.net-informations.com/dataa...-sqlserver.htm
First you must buy a Book, i think every .NET Books that i have ever read they cover that part .
http://www.codeproject.com/KB/cs/NTier.aspx
http://www.codeproject.com/KB/cs/N-Tier22.aspx
http://www.codeproject.com/KB/vb/N-T...cation_VB.aspx
Thank you
http://www.codeproject.com/KB/cs/NTier.aspx
http://www.codeproject.com/KB/cs/N-Tier22.aspx
http://www.codeproject.com/KB/vb/N-T...cation_VB.aspx
Thank you
Few companies that installed computers to reduce the employment of clerks have realized their expectations.... They now need more and more expensive clerks even though they call them "Developers" or "Programmers."
•
•
Join Date: May 2008
Posts: 144
Reputation:
Solved Threads: 9
A simple code on how to connect sql server from VB.Net via SQL connection string, and the use of ExecuteNonQuery() method to execute the command and the try and catch to catch errors during runtime.
Example information:
SQL data:
Database name: user
Table name: user_t
Columns: user_id,lname,fname,username,password,usertype
Controls:
textboxes:
textID= for Id
textlname = for last name
textfname = for first name
textusername = for user name
textpassword = for password
combobox:
comboUsertype = for user type
Code:
Dim con As New SqlClient.SqlConnection
Dim strCon as string = "Data Source=servername;Initial Catalog=user;Integrated Security=True"
Dim strCommand As String = "Insert into user_t (user_id, lname, fname, username, password, usertype) values ('" & Me.textID.text & "','" & Me.textlname.text & "', '" & Me.fname.text & "', '" & Me.textusername.text & "','" & Me.textpassword.text & "','" & Me.comboUsertype.text & "')"
Sub insert()
Try
con.ConnectionString = strCon
Dim cm As New SqlClient.SqlCommand(strCommand, con)
con.Open()
cm.ExecuteNonQuery()
MsgBox("Succesfully saved…")
Catch ex As Exception
MessageBox.Show(ex.Message, "Error")
Finally
If con.State = ConnectionState.Open Then
con.Close()
End If
End Try
End sub
Example information:
SQL data:
Database name: user
Table name: user_t
Columns: user_id,lname,fname,username,password,usertype
Controls:
textboxes:
textID= for Id
textlname = for last name
textfname = for first name
textusername = for user name
textpassword = for password
combobox:
comboUsertype = for user type
Code:
Dim con As New SqlClient.SqlConnection
Dim strCon as string = "Data Source=servername;Initial Catalog=user;Integrated Security=True"
Dim strCommand As String = "Insert into user_t (user_id, lname, fname, username, password, usertype) values ('" & Me.textID.text & "','" & Me.textlname.text & "', '" & Me.fname.text & "', '" & Me.textusername.text & "','" & Me.textpassword.text & "','" & Me.comboUsertype.text & "')"
Sub insert()
Try
con.ConnectionString = strCon
Dim cm As New SqlClient.SqlCommand(strCommand, con)
con.Open()
cm.ExecuteNonQuery()
MsgBox("Succesfully saved…")
Catch ex As Exception
MessageBox.Show(ex.Message, "Error")
Finally
If con.State = ConnectionState.Open Then
con.Close()
End If
End Try
End sub
![]() |
Similar Threads
- VB.Net and sql server connection (VB.NET)
- Connecting to SQL server DB from VB.NET app (VB.NET)
- SQL SERVER 2000 Login failure in ASP.NET (ASP.NET)
- SQL Server 2000 and VB 6 (Visual Basic 4 / 5 / 6)
- Simple ASP.Net Login Page (Using VB.Net) (ASP.NET)
- SQL server error: 18452 - connection to SQL error (Windows NT / 2000 / XP)
Other Threads in the VB.NET Forum
- Previous Thread: retrieve data from a data table in rows
- Next Thread: VB6 to VB.NET conversion help
| Thread Tools | Search this Thread |
"crystal .net .net2005 30minutes 2005 2008 access add arithmetic array assignment basic binary bing box button buttons center check code combobox component connectionstring convert cpu crystalreport data database databasesearch datagrid datagridview design dissertation dissertations dissertationthesis dosconsolevb.net dropdownlist editvb.net excel file-dialog firewall folder ftp google hardcopy image images isnumericfuntioncall login math memory mobile ms mssqlbackend mysql navigate net networking opacity output peertopeervideostreaming picturebox1 port print printpreview problemwithinstallation project record reports" reuse save savedialog searchbox serial soap storedprocedure string tcp temp text textbox timer toolbox trim updown useraccounts usercontrol vb vb.net vb.netcode vb.nettoolboxvisualbasic2008sidebar vb2008 vbnet view visual visualbasic visualbasic.net visualstudio web wpf






