holas queria pedirles un gran favor es que ando trancado aqui quiero conectarme a mi base de datos en access por que no manejo el sql bien entonces tengo este codigo y esta conectado por medio de sql como haria para cambiarlo mi base de datos esta ubicada asi
C:\WindowsApplication1\WindowsApplication1\Datos.mdb
y la tabla a la cual voy a enlazar el textbox1 se nombre
gracias de antemano
Option Explicit On
Option Strict On
' espacio de nombres para poder acceder al cliente sql
Imports System.Data
Imports System.Data.SqlClient
Public Class Form1
' enumeración para las opciones que se usarán
' para filtrar con el operador Like
Enum e_FILTER_OPTION
SIN_FILTRO = 0
CADENA_QUE_COMIENCE_CON = 1
CADENA_QUE_NO_COMIENCE_CON = 2
CADENA_QUE_CONTENGA = 3
CADENA_QUE_NO_CONTENGA = 4
CADENA_IGUAL = 5
End Enum
' cadena de conexión para SQL EXPRESS en modo local
Private Const cs As String = "Data Source=(local)\SQLEXPRESS;" & _
"Integrated Security=True;" & _
"Initial Catalog=la_base_de_datos"
'Instanciar el componente BindingSource
Private BindingSource1 As Windows.Forms.BindingSource = New BindingSource
Private Sub Form1_Load( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Try
' Inicializar la conexión y abrir
Using cn As SqlConnection = New SqlConnection(cs)
cn.Open()
' Inicializar DataAdapter indicando el sql para recuperar
'los registros de la tabla
Dim da As New SqlDataAdapter("SELECT * FROM la_tabla", cn)
Dim dt As New DataTable ' crear un DataTable
' llenarlo
da.Fill(dt)
' enlazar el DataTable al BindingSource
BindingSource1.DataSource = dt
' agregar las opciones al combobox
With (ComboBox1)
'cargar los items de opciones para filtrar
.Items.Add("No filtrar")
.Items.Add("Que comience con")
.Items.Add("Que No comience con")
.Items.Add("Que contenga")
.Items.Add("Que No contenga")
.Items.Add("Que sea igual")
.DropDownStyle = ComboBoxStyle.DropDownList
.SelectedIndex = 1
End With
End Using
' errores
Catch ex As Exception
MsgBox(ex.Message.ToString)
End Try
End Sub
Private Sub Aplicar_Filtro()
' filtrar por el campo Producto
''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim ret As Integer = Filtrar_DataGridView( _
"Producto", _
TextBox1.Text.Trim, _
BindingSource1, _
DataGridView1, _
CType(ComboBox1.SelectedIndex, e_FILTER_OPTION))
If ret = 0 Then
' si no hay registros cambiar el color del txtbox
TextBox1.BackColor = Color.Red
Else
TextBox1.BackColor = Color.White
End If
' visualizar la cantidad de registros
Me.Text = ret & " Registros encontrados"
End Sub
Function Filtrar_DataGridView( _
ByVal Columna As String, _
ByVal texto As String, _
ByVal BindingSource As BindingSource, _
ByVal DataGridView As DataGridView, _
Optional ByVal Opcion_Filtro As e_FILTER_OPTION = Nothing) As Integer
' verificar que el DataSource no esté vacio
If BindingSource1.DataSource Is Nothing Then
Return 0
End If
Try
Dim filtro As String = String.Empty
' Seleccionar la opción
Select Case Opcion_Filtro
Case e_FILTER_OPTION.CADENA_QUE_COMIENCE_CON
filtro = "like '" & texto.Trim & "%'"
Case e_FILTER_OPTION.CADENA_QUE_NO_COMIENCE_CON
filtro = "Not like '" & texto.Trim & "%'"
Case e_FILTER_OPTION.CADENA_QUE_NO_CONTENGA
filtro = "Not like '%" & texto.Trim & "%'"
Case e_FILTER_OPTION.CADENA_QUE_CONTENGA
filtro = "like '%" & texto.Trim & "%'"
Case e_FILTER_OPTION.CADENA_IGUAL
filtro = "='" & texto.Trim & "'"
End Select
' Opción para no filtrar
If Opcion_Filtro = e_FILTER_OPTION.SIN_FILTRO Then
filtro = String.Empty
End If
' armar el sql
If filtro <> String.Empty Then
filtro = "[" & Columna & "]" & filtro
End If
' asigar el criterio a la propiedad Filter del BindingSource
BindingSource.Filter = filtro
' enlzar el datagridview al BindingSource
DataGridView.DataSource = BindingSource.DataSource
' retornar la cantidad de registros encontrados
Return BindingSource.Count
' errores
Catch ex As Exception
MsgBox(ex.Message.ToString, MsgBoxStyle.Critical)
End Try
Return 0
End Function
Private Sub txt_Filtro_TextChanged( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles textbox1.TextChanged
Aplicar_Filtro()
End Sub
End Class