Jump to content

Como criar uma base de dados em access via código


programadorvb6

Recommended Posts

Olá boa noite.
Estou com um problema em criar via código VB.Net uma base de dados que contenha uma chave primaria e mais 5 campos fiz assim o código mas não consegui criar a base de dados.
Grato desde já pela vossa atenção.

Imports System.Data.OleDb
Imports System.Text

Public Class Form1
    Public Ligacao As New OleDbConnection
    Public SQL As StringBuilder

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Ligacao.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & My.Application.Info.DirectoryPath & "\Viaturas.mdb"
        SQL = New StringBuilder
        SQL.Append("CREATE TABLE Viaturas [Matriculas] NCHAR (15)  NOT NULL PRIMARYKEY, [Nome] NCHAR (25) NOT NULL,[Empresa] NCHAR (20) NOT NULL,[Piso] NCHAR (6) NOT NULL ,[Ext] NCHAR (6) NOT NULL")

        Executar1(SQL.ToString)
    End Sub

    Public Sub Executar1(ByVal pSQL As String)
        Dim Comando As OleDbCommand = Ligacao.CreateCommand
        Try
            Comando.CommandText = pSQL
            Comando.CommandType = CommandType.Text
            Comando.ExecuteNonQuery()

        Catch ex As Exception
            If Err.Number = 5 AndAlso Err.Number = 5 Then
                MessageBox.Show(ex.Message)
            Else
                MessageBox.Show("Erro descrição : " & vbCrLf & ex.Message & vbCrLf, "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        End Try
    End Sub

End Class 

______________________________________________________________________________

Que minha coragem seja maior que meu medo e que minha força seja tão grande quanto minha fé.
 

Link to comment
Share on other sites

Qual é o erro que te dá?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.