Jump to content

Inserir dados no acess a partir do vb 2005


mariogx

Recommended Posts

Boas!!!!

Tou k problemas a inserir dados na base de dados a partir du vb 2005...

o erro encontra-se no comando executenonquery..

o erro é o seguinte:

ExecuteNonQuery: Connection property has not been initialized.

o código é este:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
'inserir dados 
Dim valor As Integer 
ligação.Open() 'abrir a ligação 
Dim sql As New OleDbCommand("insert into doc(ID,Nome,Gabinete,Extensão,Telefone,DS,User,Pass) values('" & txtid.Text & "','" & txtnome.Text & "','" & txtgab.Text & "','" & txtex.Text & "','" & txttel.Text & "','" & combods.SelectedValue & "','" & txtuser.Text & "','" & txtpass.Text & "'") 
valor = sql.ExecuteNonQuery() 
ligação.Close() 'fechar a ligação 
End Sub 

HELP ME!!!

Link to comment
Share on other sites

  • 3 years later...

Dim sql As New OleDbCommand

sql = ("insert into doc(ID,Nome,Gabinete,Extensão,Telefone,DS,User,Pass) values('" & txtid.Text & "','" & txtnome.Text & "','" & txtgab.Text & "','" & txtex.Text & "','" & txttel.Text & "','" & combods.SelectedValue & "','" & txtuser.Text & "','" & txtpass.Text & "'") 

dataAdapter.InsertCommand = New OleDb.OleDbCommand(sql, ligacao)

ligacao.Open()

dataAdapter.InsertCommand.ExecuteNonQuery()

ligacao.Close()

Mas antes de tentar isto altera os caracteres especias como "ç" ou "ã"

nota : dataAdapter é uma variavel do tipo dataAdapter

Cumps

Link to comment
Share on other sites

tinha também esse problema e foi assim que resolvi.

Dim sql As New OleDbCommand

sql = ("insert into doc(ID,Nome,Gabinete,Extensão,Telefone,DS,User,Pass) values('" & txtid.Text & "','" & txtnome.Text & "','" & txtgab.Text & "','" & txtex.Text & "','" & txttel.Text & "','" & combods.SelectedValue & "','" & txtuser.Text & "','" & txtpass.Text & "'")

dataAdapter.InsertCommand = New OleDb.OleDbCommand(sql, ligacao)

ligacao.Open()

dataAdapter.InsertCommand.Connection=ligacao

dataAdapter.InsertCommand.ExecuteNonQuery()

ligacao.Close()
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.