4 Replies - 22542 Views - Last Post: 11 November 2008 - 11:43 PM Rate Topic: -----

#1 joy0686  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 22-October 08

How to Access sql database using vb.net

Post icon  Posted 22 October 2008 - 11:56 PM

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

Is This A Good Question/Topic? 0
  • +

Replies To: How to Access sql database using vb.net

#2 gymratz  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 129
  • Joined: 18-October 07

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:
		<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

Was This Post Helpful? 0
  • +
  • -

#3 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

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
Was This Post Helpful? 0
  • +
  • -

#4 Guest_dtekind*


Reputation:

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.....)
Was This Post Helpful? 0

#5 Rozie0910  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 13-October 08

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
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1