• No se han encontrado resultados

4.1 Datos Cuantitativos

4.1.2 Análisis de frecuencias en cuanto a la variable dependiente y las

4.1.2.1 Caminabilidad

The Connection object is a class provided by the .NET Framework that connects the requesting application to the data source. There are currently two flavors of the connection object with a beta for Oracle on the way.

The OleDbConnection class, found in the System.Data.OleDb namespace, supports all

OleDb Data Providers. Use the following imports statement and OleDBConnection object when using the OleDb Data providers.

... ...

Imports System.Data.OleDb Public Class MyClass

Dim objConn As OleDbConnection

... ...

You can also use the class's fully qualified name rather than importing the namespace. (You can access any class in the .NET Framework this way to make your code more readable, especially when classes with the same name exist.)

... ...

Public Class My Class

Dim objConn As System.Data.OleDb.OleDbConnection

... ...

Setting OleDbConnection Properties

The OleDbConnection object's properties can be set or retrieved in code, but they are usually

set these through a string passed to the ConnectionString property. Here is an example of a

connection string for the OleDbConnection.ConnectionString property. (This connection string will connect you to the AddressBook database we created in Chapter 8, "ASP.NET.")

... ...

Dim strConn As String

strConn = "Provider=SQLOLEDB;Data Source=localhost;" & _ "Initial Catalog=AddressBook;User Id=sa;Pwd=;"

... ...

The connection object's required properties are now stored in the strConn string. While there are several other connection properties, you'll learn about the required ones:

Provider: Name of the OleDb data provider, which is a fancy way of saying database driver.

Data Source: This is the name of the server's name where the database resides. This could just as easily be the IP address of the database server.

Initial Catalog: This is the name of the database you want to connect to. • User ID: The User Id for the database you wish to connect with.

Pwd or Password: The user's password of the User Id provided.

Once you set all the required properties you can Open the connection. Following are few ways

to build a connection object, then open a database connection.

Building a Connection Object

Let's build a quick and dirty application that won't actually do anything interesting. You'll simply open a database connection and then close it. You'll also use the connection object to extract a database schema.

1. Create a new Visual Basic windows application named "Connection Objects". 2. Add two buttons and a data grid control onto the form. Set the properties of each

Table 9-1: Control Properties for Connection Objects

Control Property Value

Button ID btnConnections

Text Connection Objects

Button ID btnGetOleDbSchemaTable

Text GetOleDbSchemaTable

DataGrid ID DataGrid1

6. Add the "Imports System.Data.OleDb" statement before the "Public Class Form1 statement" of the Windows Form.

7. Double-click the btnConnections button and build the connection string for each of our connection examples:

8. ...

...

9. ' These are only a few options for creating and opening a

connection

10. 'object.

11. Dim strConn As String

12. strConn = "Provider=SQLOLEDB;Data Source=localhost; & _

13. "Initial Catalog=AddressBook;User Id=sa;Pwd=;"

14. ...

...

15.The first connection example creates a new connection by first declaring an object as type OleDbConnection then instantiating it with the connection string:

16. ...

...

17. ' Option 1

18. Dim objConn As OleDbConnection

19. objConn = New OleDbConnection(strConn)

20. objConn.Open()

21. objConn.Close()

22. ...

...

23.Now comment out option 1 and implement option two with the following code to declare and instantiate our connection in a single statement:

24. ...

...

25. ' Option 2

26. Dim objConn As OleDbConnection = New OleDbConnection(strConn)

27. objConn.Open()

28. objConn.Close()

29. ...

...

30.Next, comment out option 2 and add option 3. (There is no significant difference between option 2 and 3 except option 3 uses the least code possible.)

31. ...

...

32. 'Option 3

33. Dim objConn As New OleDbConnection(strConn)

34. objConn.Open()

35. objConn.Close()

36. ...

37.You won't spend any more time on the connection object, but to give you a quick taste of features you may want to look into, add the following code to the

btnGetOleDbSchemaTable button's click event. Next, double-click the

btnGetOleDbSchemaTable_Click button and place the following code into it. This

will extract data about the add databases, tables, columns, or procedures depending on which option you leave uncommented:

38. ...

...

39. ' These are only a few options for creating and opening a

connection

40. 'object.

41. Dim strConn As String

42. strConn = "Provider=SQLOLEDB;Data Source=localhost;Initial " & _

43. "Catalog=AddressBook;User Id=sa;Pwd=;"

44. Dim objConn As New OleDbConnection(strConn)

45. Dim objDataTable As DataTable

46. Dim objDataSet As DataSet = New DataSet()

47.

48. objConn.Open()

49.

50. 'Getting Database Schema

51. objDataTable = objConn.GetOleDbSchemaTable( _ 52. 'OleDbSchemaGuid.Catalogs, Nothing) 53. 'objDataTable = objConn.GetOleDbSchemaTable( _ 54. 'OleDbSchemaGuid.Columns, Nothing) 55. 'objDataTable = objConn.GetOleDbSchemaTable( _ 56. 'OleDbSchemaGuid.Indexes, Nothing) 57. 'objDataTable = objConn.GetOleDbSchemaTable( _ 58. 'OleDbSchemaGuid.Procedures, Nothing) 59. 'objDataTable = objConn.GetOleDbSchemaTable( _ 60. 'OleDbSchemaGuid.Tables, Nothing) 61. 62. objDataSet.Tables.Add(objDataTable) 63. DataGrid1.DataSource = objDataSet 64. 65. objConn.Close() 66. ... ...

67.Now run the application.

SQLConnection

The SQLConnection object works very much like the OleDbConnection object except that it

requires a few minor changes. The namespace for referencing the SQL Connection object is

System.Data.SqlClient. (When building the connection string, you don't need to specify

the provider since the data provider we are using can only work with SQL Server 7 and above.) Your connection string might look something like this:

... ...

Data Source=localhost;Initial Catalog=AddressBook;User Id=sa;Pwd=;

... ...

Both the OleDb and SQL data providers can be used for accessing SQL Server 7 and above; however, as mentioned earlier, the SQL-specific provider has more direct access to SQL

Server and therefore gives much better performance. If you need to access any other data source, use the OleDb provider or a data provider specific to the desired data source.