quinta-feira, 27 de outubro de 2011

on

Imports System.Data.Sql
Imports System.Data.SqlClient

Public Class Form1

    Dim con As String '= "C:\SQL Server 2000 Sample Databases\NORTHWND.MDF"

    Dim ds As DataSet

    Private Sub exibeDados(ByVal selectCommand As String)
        Try                       

            Dim connectionString As String

            connectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=" & con & ";Integrated Security=True;Connect Timeout=30;User Instance=True"

            Dim dataAdapter As New SqlDataAdapter(selectCommand, connectionString)

            ' Cria um command builder para gerar os SQL para atualizar, incluir e
            ' deletar baseados no selectCommand.

            Dim commandBuilder As New SqlCommandBuilder(dataAdapter)
            Dim table As New DataTable()
            table.Locale = System.Globalization.CultureInfo.InvariantCulture
            dataAdapter.Fill(table)
            BindingSource1.DataSource = table           

        Catch ex As SqlException
            MessageBox.Show("Para rodar este exemplo. Substitua a string de conexão usada _ para um valor válido para o seu sistema.")
        End Try
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try

            openFileDialog1.RestoreDirectory = True
            openFileDialog1.Title = "Find Files"
            OpenFileDialog1.Filter = "files (*.mdf; *.MDF)|*.mdf;*.MDF"
            openFileDialog1.FilterIndex = 2
            openFileDialog1.RestoreDirectory = True

            If Me.OpenFileDialog1.ShowDialog = DialogResult.OK Then
                con = openFileDialog1.FileName
            Else
                MsgBox("Operação cancelada.", MsgBoxStyle.Information)
            End If

            Me.dgv.DataSource = Me.BindingSource1
            exibeDados("SELECT * FROM Customers WHERE CompanyName LIKE 'A%'")

            Label1.Text = con

        Catch ex As Exception

        End Try
    End Sub

End Class


0 comentários:

Postar um comentário