Jump to content

Duda con un insert en vb.net


Recommended Posts

Queria ver si habia la posibilidad de que alguien me ayudara con este codigo de un simple insert que la caga no le puedo encontrar el error ya que corre todo bien pero no inserta los datos

Imports System.Data
Imports System.Data.SqlClient
Public Class Form1
   Dim con As SqlConnection
   Dim cmd As SqlCommand
   Dim dr As SqlDataReader
   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    cmd = New SqlCommand("insert into usuarios(usuario,pass) values(@Data1,@Data2) ", con)
    cmd.Parameters.AddWithValue("@Data1", TextBox1.Text)
    cmd.Parameters.AddWithValue("@Data2", TextBox2.Text)
    cmd.ExecuteNonQuery()
    MsgBox("Successfully inserted ")
   End Sub
   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    con = New SqlConnection("Data Source=NTB\SQLExpress;Integrated Security=true; AttachDbFilename=|DataDirectory|\insert.mdf;User Instance=true;")
    con.Open()
   End Sub
End Class

Link to comment
Share on other sites

Trata de seguir este ejemplo:

 


DIM q AS STRING
DIM orden as OLEDBCOMMAND
q = "insert into mitabla(clave,nombre,edad) values(@CLAVE, @NOMBRE, @EDAD)"
orden = NEW OLEDBCOMMAND(q, coneccion)
orden.Parameters.Add(new OleDbParameter("@CLAVE", OleDbType.Integer))
orden.Parameters("@CLAVE").Value = CLAVE.Text
orden.Parameters.Add(new OleDbParameter("@NOMBRE", OleDbType.VarWChar, 20))
orden.Parameters("@NOMBRE").Value = NOMBRE.Text
orden.Parameters.Add(new OleDbParameter("@EDAD", OleDbType.Integer))
orden.Parameters("@EDAD").Value = EDAD.Text
orden.Connection.Open()
orden.ExecuteNonQuery()
orden.Connection.Close()

 

O este enlace de MS: http://support.micro...om/kb/322045/es

 

 

O este: http://www.elguille....nes_insert.aspx

 

 

 

Saludos :krider:

Edited by AshWilliams
Link to comment
Share on other sites

  • 1 month later...

UNA FORMA ES HACERLO EN CAPAS.

 

CAPA CONEXION.

 

Imports System.Windows.Forms
Imports System.Data.OleDb
Public Class Conexion
   'Atributos de la Conexion
   Private nombreBaseDatos As String
   Private nombreTabla As String
   Private cadenaConexion As String
   Private user As String
   Private password As String
   Private sentenciaSQL As String
   Private esQuery As Boolean
   'Clases para la Conexión
   Private dbConnection As OleDbConnection
   Private dbDataAdapter As OleDbDataAdapter
   Private dbDataSet As Data.DataSet
   Private dbDataTable As Data.DataTable
   'Construtor
   Public Sub New()
    Me.nombreBaseDatos = ""
    Me.nombreTabla = ""
    Me.cadenaConexion = ""
    Me.user = ""
    Me.password = ""
    Me.esQuery = False
    Me.sentenciaSQL = ""
   End Sub
   'Accesores
   Public Function getNombreBaseDatos() As String
    Return Me.nombreBaseDatos
   End Function
   Public Function getNombreTabla() As String
    Return Me.nombreTabla
   End Function
   Public Function getSentenciaSQL() As String
    Return Me.sentenciaSQL
   End Function
   Public Function getUser() As String
    Return Me.user
   End Function
   Public Function getPassword() As String
    Return Me.password
   End Function
   Public Function getCadenaConexion() As String
    Return Me.cadenaConexion
   End Function
   Public Function getEsQuery() As Boolean
    Return Me.esQuery
   End Function
   Public Function getDbDataSet() As Data.DataSet
    Return Me.dbDataSet
   End Function
   'Mutadores
   Public Sub setNombreBaseDatos(ByVal nombreBaseDatos)
    Me.nombreBaseDatos = nombreBaseDatos
   End Sub
   Public Sub setNombreTabla(ByVal nombreTabla)
    Me.nombreTabla = nombreTabla
   End Sub
   Public Sub setSentenciaSQL(ByVal sentenciaSQL)
    Me.sentenciaSQL = sentenciaSQL
   End Sub
   Public Sub setCadenaConexion(ByVal cadenaConexion)
    Me.cadenaConexion = cadenaConexion
   End Sub
   Public Sub setUser(ByVal user)
    Me.user = user
   End Sub
   Public Sub setPassword(ByVal password)
    Me.password = password
   End Sub
   Public Sub setEsQuery(ByVal esQuery As Boolean)
    Me.esQuery = esQuery
   End Sub
   Public Sub setDbDataSet(ByVal dbDataSet As Data.DataSet)
    Me.dbDataSet = dbDataSet
   End Sub
   Public Function getDbDataTable() As Data.DataTable
    Return Me.dbDataTable
   End Function
   Public Sub setDbDataTable(ByVal dbDataTable As Data.DataTable)
    Me.dbDataTable = dbDataTable
   End Sub
   'Metodo para Abrir la Conexion
   Public Sub abrirConexion()
    Try
	    Me.dbConnection.Open()
    Catch ex As Exception
	    MessageBox.Show("Error. Al Abrir la Conexión: " & _
					    ex.Message, "Error de Conexión", _
					    MessageBoxButtons.OK, _
					    MessageBoxIcon.Error)
    End Try
   End Sub
   'Metodo para Cerrar la Conexion
   Public Sub cerrarConexion()
    Try
	    Me.dbConnection.Close()
    Catch ex As Exception
	    MessageBox.Show("Error. Al Cerrar la Conexión: " & _
					    ex.Message, "Error de Conexión", _
					    MessageBoxButtons.OK, _
					    MessageBoxIcon.Error)
    End Try
   End Sub
   'Metodo Principal de la Conexión
   Public Sub conectar()
    'Crear una Nueva conexion
    Try
	    Me.dbConnection = New OleDbConnection(Me.getCadenaConexion)
    Catch ex As Exception
	    MessageBox.Show("Error. Al Crear la Conexión: " & _
						    ex.Message, "Error de Conexión", _
						    MessageBoxButtons.OK, _
						    MessageBoxIcon.Error)
	    Exit Sub
    End Try
    'Abrir la Conexion
    Me.abrirConexion()
    'Verificar si se ejecuta un QUERY o un UPDATE
    If Me.getEsQuery = True Then
	    'EJECUTAR QUERY
	    Me.dbDataSet = New Data.DataSet
	    Me.dbDataTable = New Data.DataTable
	    Try
		    Me.dbDataAdapter = New OleDbDataAdapter(Me.getSentenciaSQL, Me.dbConnection)
		    'Llenar el DataSet
		    Me.dbDataAdapter.Fill(Me.getDbDataSet(), Me.getNombreTabla)
		    'Llenar el DataTable
		    Me.dbDataAdapter.Fill(Me.dbDataTable)
	    Catch ex As Exception
		    MessageBox.Show("Error. Al Crear la Consulta: " & _
							    ex.Message, "Error de Conexión", _
							    MessageBoxButtons.OK, _
							    MessageBoxIcon.Error)
	    End Try
    Else
	    Try
		    'EJECUTAR UPDATE
		    Dim comando As New OleDbCommand(Me.getSentenciaSQL(), Me.dbConnection)
		    'Ejecutar Comando
		    comando.ExecuteNonQuery()
	    Catch ex As Exception
		    MessageBox.Show("Error. Al Crear la Consulta: " & _
							    ex.Message, "Error de Conexión", _
							    MessageBoxButtons.OK, _
							    MessageBoxIcon.Error)
	    End Try
    End If
   End Sub

End Class

 

CAPA DE NEGOCIO en donde declaro todas las interacciones con mi base de datos:

 

 

 

Imports CapaConexion
Imports CapaDTO
Public Class NegocioComuna
 Private cnn As New Conexion

 Private Sub configurarConexion()
   Me.cnn.setNombreBaseDatos("C:\Hospital_Clinico.mdb")
   Me.cnn.setCadenaConexion("Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & Me.cnn.getNombreBaseDatos)
   Me.cnn.setNombreTabla("Comunas")
   Me.cnn.setUser("alumnosec2")
   Me.cnn.setPassword("sa")
 End Sub

 'METODO DE INGRESO DE COMUNA
 Public Sub IngresarComuna(ByVal ingresoCom As Comuna)
   Me.configurarConexion()
   Me.cnn.setSentenciaSQL("Insert into " & Me.cnn.getNombreTabla & " Values('" & ingresoCom.getRegion & "', '" & ingresoCom.getComuna & "')")
   Me.cnn.setEsQuery(False)
   Me.cnn.conectar()
   Me.cnn.cerrarConexion()
 End Sub
End Class

 

 

 

BOTON INSERTAR

 

    'METODO PARA GUARDAR COMUNAS
    Dim auxNegocio As New NegocioComuna
    Dim IngresarComun As New Comuna
    IngresarComun.setRegion(Trim(Me.cboRegion.Text))
    IngresarComun.setComuna(Trim(Me.txtComuna.Text))

    auxNegocio.IngresarComuna(IngresarComun)
    MessageBox.Show("Comuna Ingresada", "Ingreso de Datos", MessageBoxButtons.OK, MessageBoxIcon.Information)

 

 

CAPA DE DATOS

 

Public Class Comuna
   'ATRIBUTOS
   Private region As String
   Private comuna As String

   'CONSTRUCTOR SIN PARAMETROS
   Public Sub New()
    Me.region = ""
    Me.comuna = ""
   End Sub
   'CONSTRUCTOR CON PARAMETROS
   Public Sub New(ByVal region As String, ByVal comuna As String)
    Me.region = region
    Me.comuna = comuna
   End Sub

   'METODOS ACCESADORES
   Public Function getRegion() As String
    Return Me.region
   End Function

   Public Function getComuna() As String
    Return Me.comuna
   End Function

   'METODOS MUTADORES
   Public Sub setRegion(ByVal region As String)
    Me.region = region
   End Sub

   Public Sub setComuna(ByVal comuna As String)
    Me.comuna = comuna
   End Sub
End Class

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...