Insert of c # with access

1

I have an error with the insert to the Access database, when I run the program and fill in all the fields and I click save it appears that it was saved correctly and does not show any error in the exception, and when I go to see in the database I realize that the saved data does not appear, the Insert I have done it in several ways with querys and with the dataset of vs.

I would like to know why you do not show me the data in Access, and I do not get the error that something is wrong.

This is the code I am using with the query, and I have also done it with the data set and it does not allow me to insert it in Access:

int identificacion = (int) cbIdentificacion.SelectedValue;
int categoria = (int) cbtipoTercero.SelectedValue;
DateTime fecha = DateTime.Today;
String estado = "activo";
try {
    //crear conexion
    OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\andresfelipe\Desktop\Melonada\Melonada\BD\Melonada.accdb");

    //conectar a la bd
    con.Open();

    //query
    String sql = "INSERT INTO Tercero VALUES('" + this.txtIdentificacion.Text + "'," + identificacion + ",'" + 
                 this.txtNombre.Text + "','" + this.txtPrimerApellido.Text + "','" + this.txtSegundoApellido.Text + "','" + 
                 this.cbSexo.Text + "','" + this.txtTelefono.Text + "','" + this.txtCelular.Text + "','" + this.txtDireccion.Text + "'," + 
                 categoria + ",'" + fecha + "','" + estado + "');";
    OleDbCommand comando = new OleDbCommand(sql, con);
    comando.ExecuteNonQuery();

    MessageBox.Show("El usuario se ingreso satisfactoriamente");
}
catch(Exception ex) {
    MessageBox.Show("no se ingreso" + ex.ToString());
}

// ese es del dataset:
this.terceroTableAdapter1.InsertTercero(this.txtIdentificacion.Text,
identificacion, this.txtNombre.Text, this.txtPrimerApellido.Text,
this.txtSegundoApellido.Text, this.cbSexo.Text, this.txtTelefono.Text, 
this.txtCelular.Text, this.txtDireccion.Text, categoria, fecha, estado);
    
asked by Felipe 25.07.2016 в 04:44
source

1 answer

1

I would recommend that you use parameters when you make the insert

 string connstring = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\andresfelipe\Desktop\Melonada\Melonada\BD\Melonada.accdb";
 using(OleDbConnection con = new OleDbConnection(connstring))
 {
    con.Open();

    string sql = "INSERT INTO Tercero (identificacio, tipoidentificacion, nombre, ...) VALUES(@identificacion, @tipoIdentificacion,@nombre, );";

    OleDbCommand comando = new OleDbCommand(sql, con);
    comando.Parameters.AddWithValue("@identificacion", txtIdentificacion.Text);
    comando.Parameters.AddWithValue("@tipoIdentificacion", identificacion);
    comando.Parameters.AddWithValue("@nombre", txtNombre.Text);

     //resto parametros

    comando.ExecuteNonQuery();
}

In addition, it would be advisable to define the names of the fields, because if for some reason these change their order, the insetrt you define will be affected.

Imagine that the identification combo would be related to some type that is allowed to be selected

    
answered by 26.07.2016 в 07:44