Documentos de Académico
Documentos de Profesional
Documentos de Cultura
VB.net
En primer lugar debemos realizar las importaciones de las clases que necesitamos
para realizar la conexin a una base de datos SQL Server con VB.net:
Imports System.Data
Imports System.Data.OleDb
Imports System.Data.SqlClient
Try
Dim s As String = ("SELECT * FROM Alumnes")
connexio = New OleDbConnection(myConnectionString)
myCommand = New OleDbCommand(s)
myCommand.Connection = connexio
connexio.Open()
Dim myReader As OleDbDataReader = myCommand.ExecuteReader()
While myReader.Read()
Dim NOM As String = myReader("NOM")
Dim COGNOM As String = myReader("COGNOM")
End While
Catch exc As Exception
Throw New GestorExcepcio(exc.Message)
End Try
Try
Dim sent As String = ("INSERT INTO Alumnes VALUES ('JORDI','NAVES')")
Dim connexio As New OleDbConnection(myConnectionString)
myCommand = New OleDbCommand(sent)
myCommand.Connection = connexio
connexio.Open()
myCommand.ExecuteNonQuery()
connexio.Close()
Catch exc As Exception
Throw New GestorExcepcio(exc.Message)
End Try
Y finalmente para modificar un registro mediante un update:
Try
Dim s As String = ("UPDATE Alumnes SET nom='JORDI' Where id=1")
connexio = New OleDbConnection(myConnectionString)
myCommand = New OleDbCommand(s)
myCommand.Connection = connexio
connexio.Open()
myCommand.ExecuteNonQuery()
connexio.Close()
Catch exc As Exception
Throw New GestorExcepcio(exc.Message)
End Try
http://www.jnsoft.net/blog/connexion-base-datos-sqlserver-con-vb-net/