Hi,
this is joy,a beginner in vb.net.Presently i know how to access msaccess database using vb.net,for which code is con.ConnectionString="PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\username\My Documents\joy.mdb".Now this for ms access but if i want to access sql database then what will i have to type after con.ConnectionString?And will the rest of the code will be same?That is code for opening sql database and msaccess database in vb.net.Please help!!!Thank You
How to Access sql database using vb.net
Page 1 of 14 Replies - 12456 Views - Last Post: 11 November 2008 - 11:43 PM
Replies To: How to Access sql database using vb.net
#2
Re: How to Access sql database using vb.net
Posted 23 October 2008 - 08:47 AM
This may not be the perfect way, but I'll share the way that I do it since no one else has responded yet.
I use the web.config to store my connection information so that it is hidden from the users.
Here is an example:
Initial Catalog is the database name.
UID and PWD is the username and password to authenticate, such as using the SA account.
Data Source is the name of your sql server.
Then within the vb.net code I declare my variables.
Then I open up my connection
I enter my command
I do something with my command...
And then I close my reader
Finally I close my connection
This is using vb.net and asp.net for programming. If you aren't doing a web application I would assume it is very close to the same EXCEPT you'd probably put the information in my web.config directly into the connection string.
I use the web.config to store my connection information so that it is hidden from the users.
Here is an example:
<add key="ConnectionStringWF" value="Initial Catalog=databasename;uid=usernamehere;pwd=passwordhere;Data Source=sqlservername;Network Library=dbmssocn;"/>
Initial Catalog is the database name.
UID and PWD is the username and password to authenticate, such as using the SA account.
Data Source is the name of your sql server.
Then within the vb.net code I declare my variables.
Dim db_connection As SqlConnection Dim db_command As SqlCommand Dim datareader As SqlDataReader
Then I open up my connection
db_connection = New SqlConnection(ConfigurationSettings.AppSettings("ConnectionStringWF"))
db_connection.Open()
I enter my command
db_command = New SqlCommand("select * from Clients where ID = '" & lbl_client_id.Text & "'", db_connection)
datareader = db_command.ExecuteReader()
I do something with my command...
While datareader.Read
lbl_c_name.Text = datareader("Name").ToString
End While
And then I close my reader
datareader.close
Finally I close my connection
db_connection.close()
This is using vb.net and asp.net for programming. If you aren't doing a web application I would assume it is very close to the same EXCEPT you'd probably put the information in my web.config directly into the connection string.
This post has been edited by gymratz: 23 October 2008 - 08:48 AM
#3
Re: How to Access sql database using vb.net
Posted 23 October 2008 - 08:52 AM
Check out the tutorial for Sql Server specific controls to use when establishing a database connection.
SQL Basics In VB.Net
SQL Basics In VB.Net
#4 Guest_dtekind*
Re: How to Access sql database using vb.net
Posted 11 November 2008 - 02:03 PM
[code] well i think it's not hard if you were using the database that you're creat from vb.net but if you are going to access into a database exist in a server you need to creat that code :
dim cn as new sqlclien.sqlconnection("data source = server_name;initial catalog= databse_name ;user id= ur_user; password=ur_password")
cn.open()
dim cmd as new sqlclient.sqcommand("select filed,..... from table_name",cn)
dim dr as sqldatareader=cmd.executereader()
dr.read()
' now try to use the control where you want to show your data (listbox,datagrid.....)
dim cn as new sqlclien.sqlconnection("data source = server_name;initial catalog= databse_name ;user id= ur_user; password=ur_password")
cn.open()
dim cmd as new sqlclient.sqcommand("select filed,..... from table_name",cn)
dim dr as sqldatareader=cmd.executereader()
dr.read()
' now try to use the control where you want to show your data (listbox,datagrid.....)
#5
Re: How to Access sql database using vb.net
Posted 11 November 2008 - 11:43 PM
u can check at this site...
in this site have tutorial with vb net..
hope u can learn some thing..
GOOD LUCK...
http://www.vbmysql.c...tutorial-part-3
it has many toturial u can just can change the number at the back like this
---> http://www.vbmysql.c...tutorial-part-2
or
---->http://www.vbmysql.com/articles/vbnet-mysql-tutorials/the-vbnet-mysql-tutorial-part-4
in this site have tutorial with vb net..
hope u can learn some thing..
GOOD LUCK...
http://www.vbmysql.c...tutorial-part-3
it has many toturial u can just can change the number at the back like this
---> http://www.vbmysql.c...tutorial-part-2
or
---->http://www.vbmysql.com/articles/vbnet-mysql-tutorials/the-vbnet-mysql-tutorial-part-4
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote




|