Please support our VB.NET advertiser: Programming Forums
Views: 7381 | Replies: 9
![]() |
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:
Rep Power: 0
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
try this:
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:
Rep Power: 0
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: 21
Reputation:
Rep Power: 1
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
Location: India
Posts: 130
Reputation:
Rep Power: 0
Solved Threads: 7
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
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 3 (0 members and 3 guests)






Linear Mode