1,105,328 Community Members

Sql server connection using code in VB.NET

Member Avatar
edcaryl
Newbie Poster
1 post since Jul 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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

Member Avatar
Ezzaral
Posting Sage
7,431 posts since May 2007
Reputation Points: 2,714 [?]
Q&As Helped to Solve: 953 [?]
Skill Endorsements: 31 [?]
Moderator
Featured
 
-1
 
Member Avatar
arjunsasidharan
Practically a Posting Shark
812 posts since Aug 2006
Reputation Points: 297 [?]
Q&As Helped to Solve: 20 [?]
Skill Endorsements: 0 [?]
 
1
 

Try this

"Mod Alert" - Move the thread to VB.NET Forum.

Member Avatar
MarianaM
Newbie Poster
1 post since Jul 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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;"

Member Avatar
kap_sharma
Newbie Poster
2 posts since Aug 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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.

Member Avatar
bruce2424
Newbie Poster
23 posts since Jun 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password"
connection = New SqlConnection(connetionString)

http://vb.net-informations.com/dataadapter/dataadapter-dataset-sqlserver.htm

Member Avatar
vuyiswamb
Posting Whiz
316 posts since Mar 2007
Reputation Points: 17 [?]
Q&As Helped to Solve: 14 [?]
Skill Endorsements: 0 [?]
 
0
 

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-Tier_Application_VB.aspx

Thank you

Member Avatar
sierrainfo
Junior Poster
144 posts since May 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 9 [?]
Skill Endorsements: 0 [?]
 
1
 

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

Member Avatar
Tseli
Newbie Poster
1 post since Nov 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi, im trying to save,update and delete records from access database using vb 2005, Please help wit the ciding

Member Avatar
sierrainfo
Junior Poster
144 posts since May 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 9 [?]
Skill Endorsements: 0 [?]
 
0
 
Member Avatar
xcorpionxting
Newbie Poster
10 posts since Apr 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I am having a similiar problem... I can connect to a local MySql Database using:

SQLConn.ConnectionString = "Data Source=servername;" & _
"Initial Catalog=databasename;" & _
"User ID=username;" & _
"Password=userpassword;"

Where servername is "localhost". I need to make a connection to an online database (same data structure). What should I put at the servername in that case?

Member Avatar
vuyiswamb
Posting Whiz
316 posts since Mar 2007
Reputation Points: 17 [?]
Q&As Helped to Solve: 14 [?]
Skill Endorsements: 0 [?]
 
0
 

The Server name cant be localhost. The Server name is the instance of SQL not the name of the Computer. a lot of people confuses this. There is a Difference between Data Source and servername. The Data Source is the SQL instance and the Servername is the name of the computer. in the Data Source should be the servername display when you try to connect to the sql server using SQl management studio. Copy that and put it here and it will work.

Kind Regards


Vuyiswa Maseko

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

cul stuff

Member Avatar
Netcode
Veteran Poster
1,049 posts since Jun 2009
Reputation Points: 33 [?]
Q&As Helped to Solve: 86 [?]
Skill Endorsements: 8 [?]
 
-1
 
dim con as sqlconnection = new sqlconnection( _
"server= server name; database= d-basename; user id = server id; password= server password;")
Member Avatar
__avd
Posting Genius (adatapost)
6,703 posts since Oct 2008
Reputation Points: 1,811 [?]
Q&As Helped to Solve: 1,272 [?]
Skill Endorsements: 61 [?]
Moderator
 
0
 

We are glad you got it helpful.Please do not resurrect old threads. If you have any questions please ask. You are welcome to start your own threads. Please read the rules before posting again - http://www.daniweb.com/forums/thread78223.html and rules.

Thread Locked.

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