VB.NET 2015 - Criar e Conectar Banco de Dados Sql Server 2014 (Parte 1) (Vídeo)
Autor: Melqui Serrano Arimuya | Publicado: 2016-01-06T16:11:00-04:00






Olá amigos,


Começamos o ano, com a novidade de trabalhar em Sql, espero que neste artigo seja de grande ajuda, os anteriores artigos já temos trabalhado muito sob, o desenvolvimento de Sql e poucas vezes temos usado este banco de dados, mas vamos ampliar o conhecimento de SQL, como pode ver neste artigo existe muitas mudanças de como trabalhar a conexão do banco de dados, a Microsoft mudou sua forma de trabalho, isto deixou muito complexo algumas mudanças, como por exemplo a localização e o string de programação do banco de dados, existem muitas outras formas de trabalhar com Sql porem eu aqui usarei uma forma de trabalhar para construir o meu projeto Controle de Recados 2016.




Vamos facilitar ainda mais este trabalho, para isso criamos um modulo de nome mdl_conexao

 

  
Imports System.Data.SqlClient

Module mdl_conexao



&nbsp;&nbsp;&nbsp; Public Function bd_conexao() As SqlConnection

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim sql As String = "Data
  Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename="

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sql += "C:\Users\Arimuya\documents\visual
  studio 2015\Projects\ControleRecadosSQL2016"

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sal += "\ControleRecadosSQL2016\bdregistro.mdf;Integrated
  Security=True"

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Return New SqlConnection(sql)

&nbsp;&nbsp;&nbsp; End Function



&nbsp;&nbsp;&nbsp; 'variavels

&nbsp;&nbsp;&nbsp; Public dt_tempo As String

End Module

 


e dentro do meu formulário frmchamdas


 

  
Imports System.Data.SqlClient

Public Class frmRegistroChamadas



&nbsp;&nbsp;&nbsp; Public Sub FormatarDgvDados()

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'titulo

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  DgvDados.Columns(0).HeaderText = "Codigo"

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DgvDados.Columns(1).HeaderText
  = "De"

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DgvDados.Columns(2).HeaderText = "Para"

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DgvDados.Columns(3).HeaderText = "Telefone"

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DgvDados.Columns(4).HeaderText = "Assunto"

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DgvDados.Columns(5).HeaderText = "Observações"

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DgvDados.Columns(6).HeaderText
  = "Data de registro"

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'alinhamento

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  DgvDados.Columns(0).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  DgvDados.Columns(1).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  DgvDados.Columns(2).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DgvDados.Columns(3).DefaultCellStyle.Alignment
  = DataGridViewContentAlignment.MiddleCenter

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  DgvDados.Columns(4).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  DgvDados.Columns(5).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  DgvDados.Columns(6).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter



&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'automatizar espaço

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  DgvDados.Columns(0).AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  DgvDados.Columns(1).AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  DgvDados.Columns(2).AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  DgvDados.Columns(3).AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  DgvDados.Columns(4).AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  DgvDados.Columns(5).AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  DgvDados.Columns(6).AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells



&nbsp;&nbsp;&nbsp; End Sub



&nbsp;&nbsp;&nbsp; Public Sub CarregarDados()

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Using cn As SqlConnection = bd_conexao()

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Try

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cn.Open()

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim sql As String = "Select * From dados"

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim cmd As SqlCommand = New SqlCommand(sql, cn)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim da As SqlDataAdapter = New SqlDataAdapter(cmd)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim dt As DataTable = New DataTable

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; da.Fill(dt)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DgvDados.DataSource = dt



&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'formatar datagrid

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Call
  FormatarDgvDados()

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Catch ex As Exception

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MsgBox(ex.Message)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Finally

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cn.Close()

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End Try

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End Using

&nbsp;&nbsp;&nbsp; End Sub

&nbsp;&nbsp; &nbsp;&nbsp;End Class

 





Isto facilita de como trabalhar com Sql, caso tenha duvida acompanhe o Vídeo que ira a entender mas detalhado com desenvolvimento da criação do banco de dados e a conexão.



espero que seja de grande ajuda



Veja também


VB.NET 2015 - Carregando dados por linha com SQL Compact (Banco de Dados Local) (Vídeo)


VB.NET 2010 - Conexão de Banco de Dados de SQL 2008 Cadastro, Consulta, Modificação, Deletar, Pesquisar


VB.NET 2010 - Consulta de Dados de SQL SERVER com parâmetros no DataGrid - Versão Framework 4.0 e 4.5 (gerado)



VB.NET 2013 - Autocompletar TextBox e ComboBox com Sql Server 2012




VB.NET 2013 - Filtrar e Registrar dados com Access 2013 e SQL Server 2012 (Vídeo)



VB.NET 2013 - Autocompletar TextBox e ComboBox com Sql Server 2012