RE: [Programa] Asp.net
este e o codigo que tenho para fazer a ligação a base de dados
Citar:Imports System.Data.OleDb
Imports System.IO.Stream
Imports System.IO
Partial Public Class WebForm1
Inherits System.Web.UI.Page
Dim codigo, nome, data_nascimento, morada, localidade, codpostal, bi, contacto, mail, ano, turma, sexo, pass As TextBox
Dim cn As OleDbConnection
Dim cmd As OleDbCommand
Dim dr As OleDbDataReader
Dim sql As String
Dim strconeccao As String
Dim userMsg As String
Dim ds As DataSet
Dim dc As DataColumn
Dim da As OleDbDataAdapter
Dim t As String
Dim objConnection As OleDbConnection 'declara objecto para conexão
Dim connString As String
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
ligarbd()
End Sub
Sub ligarbd()
connString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE= C:\Documents and Settings\Filipe\Ambiente de trabalho\PAP\Projectos\PAP\Exame_online.mdb"
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
sql = "INSERT INTO aluregis(codigo, nome, data_nascimento, morada, localidade,codpostal, bi, contacto, mail, ano, turma, sexo, pass)" & " values ('" & codigo.Text & "', '" & nome.Text & "', '" & data_nascimento.Text & "', '" & morada.Text & "', '" & localidade.Text & "', '" & codpostal.Text & "', '" & bi.Text & "', '" & contacto.Text & "', '" & mail.Text & "', '" & ano.Text & "', '" & turma.Text & "', '" & sexo.Text & "', '" & pass.Text & "')"
'sql = "insert into aluregis values(" & " '" & codigo.Text & "'," & " '" & nome.Text & "'," & " '" & data_nascimento.Text & "'," & "'" & morada.Text & "'," & "'" & localidade.Text & "'," & "'" & codpostal.Text & "'," & "'" & bi.Text & "'," & "'" & contacto.Text & "'," & "'" & mail.Text & "'," & "'" & ano.Text & "'," & "'" & turma.Text & "'," & "'" & sexo.Text & "'," & "'" & pass.Text & "')"
ligarbd()
objConnection.Open() 'abre a conexão
objConnection = New OleDbConnection(connString) 'cria conexão
Try
cn = New OleDb.OleDbConnection(connString)
cn.Open()
cmd = New OleDbCommand(sql, cn)
cmd.ExecuteNonQuery()
Catch ex As Exception
Response.Write(ex.ToString)
Finally
cn.Close()
End Try
MsgBox("O Aluno " & nome.Text & " foi inserido com sucesso")
'limpar()
End Sub
|