Funcion conectar a BD SQL Server (sqlconection)


/ Published in: VB.NET
Save to your folder(s)

Conexión a base de datos con los parametros que llega del contructor


Copy this code and paste it in your HTML
  1. Imports System
  2. Imports System.IO
  3. Imports System.Text
  4. Imports System.Data
  5. Imports System.Data.SqlClient
  6.  
  7. Public Class DBSQLServer
  8.  
  9. #Region "Properties"
  10. Private con As New SqlConnection
  11. Private cmd As New SqlCommand
  12. Private reader As SqlDataReader
  13. Private da As SqlDataAdapter
  14.  
  15. Public Property StringConnection As String = ""
  16. Public Property Host() As String
  17. Public Property DBName() As String
  18. Public Property User() As String
  19. Public Property Pass() As String
  20. Public Property Port() As String = 3306
  21.  
  22. #End Region
  23.  
  24. #Region "Constructores"
  25. Public Sub New(ByVal host As String, ByVal user As String, ByVal pass As String, ByVal DBName As String, ByVal Port As String)
  26. Me.Host = host
  27. Me.User = user
  28. Me.Pass = pass
  29. Me.DBName = DBName
  30. Me.Port = Port
  31. End Sub
  32.  
  33. Public Sub New(ByVal host As String, ByVal user As String, ByVal pass As String, ByVal DBName As String)
  34. Me.Host = host
  35. Me.User = user
  36. Me.Pass = pass
  37. Me.DBName = DBName
  38. End Sub
  39.  
  40. Public Sub New(ByVal strConnection As String)
  41. Me.StringConnection = strConnection
  42. End Sub
  43.  
  44. #End Region
  45.  
  46. #Region "Functions"
  47. ''' <summary>
  48. ''' conexion a la base de datos segun los parametros de constructor.
  49. ''' </summary>
  50. ''' <returns></returns>
  51. ''' <remarks></remarks>
  52. Private Function conecctedDB() As SqlConnection
  53. Try
  54. If (Me.StringConnection.Trim().Length > 0) Then
  55. Me.con = New SqlConnection(Me.StringConnection)
  56. Else
  57. Me.con = New SqlConnection(String.Format("Data Source={0}; Integrated Security=False; Id={1}; Password={2}; Catalog={3};", Me.Host, Me.User, Me.Pass, Me.DBName))
  58. End If
  59. Me.con.Open()
  60. Catch ex As SqlException
  61. Throw New System.Exception("Error de conexion a la basde de datos: (conectarBase)", ex)
  62.  
  63. End Try
  64.  
  65. Return Me.con
  66.  
  67. End Function
  68.  
  69.  
  70. #End Region
  71.  
  72. End Class

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.