Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I had a problem with conection access in C# or could be the insert

Status
Not open for further replies.

coblan19

Programmer
Dec 10, 2003
3
CR
i had this code but the connection didnt work why?
please help me
or could be the insert had a error....

public void insertar(strBases info)
{ OleDbConnection databaseConnection = new OleDbConnection(oleDbConnectionString);
string strSQLStatement = "INSERT INTO DBO.Empresapig (id_empresa, nom_empresa, num_tarjeta, cons_prom_mensual, porce_financiacion, comision_intercambio, comision_vendedor, fondo_premiacion, costo_financiamiento, porcentaje_mora, tasa_interes_mensual,gasto_operacion, costo_plastico, embozado, letra_cambio, logo_marca, prospectacion, procesamiento, consulta_infobot, alquiler_datafono, impresion_estado, envio_email) " +
info.id_emp + "','" + info.nombre_emp + "','" + info.n_tarjetas + "','" + info.consumo_prom + "','" + info.porcentaje_f + "','" + info.comision_inter + "','" + info.comision_ven + "','" + info.fondo_pre + "','" + info.costo_fin + "','" + info.porc_mora + "','" + info.tasa_intmens + "','" + info.gas_operacion + "','" + info.cos_plastico + "','" + info.embozado + "','" + info.letcontrato +
"','" + info.logo_marc + "','" + info.prospec + "','" + info.procesa + "','" + info.con_infobot + "','" + info.alq_data + "','" + info.id_emp + "','" + info.imp_formulario + "','" + info.envio_correo + ";";
OleDbCommand databaseCommand = new OleDbCommand(strSQLStatement, databaseConnection);
databaseCommand.CommandType = CommandType.Text;
try
{ databaseConnection.Open();
int numRows = databaseCommand.ExecuteNonQuery();
}
catch (Exception e)
{ Console.WriteLine("****** Caught an exception:\n{0}",
e.Message);
}
finally
{
databaseConnection.Close();
}
}
 
Your syntax of the insert is wrong. (missing the VALUES keyword)

INSERT INTO table_name (column1, column2,...)
VALUES (value1, value2,....)

Also make sure you only place ' ' around string values not integers.
 
Dear Coblan19,
As far as i'm concerned you should follow the following steps in dealing with DB:
1. Create a connection. which i don't know if you defined it previously as 'oleDbConnectionString' or you didn't yet.
2. Create the statement, however it's a SELECT, UPDATE, DELETE, INSERT,... and it's better to try it separately on your DB. Like trying it using Query Analyzer in SQL Server or on SQL+ on ORACLE DB.
3. Execute and get the result either records if you'r querying or number of rows affected if it's other.

In the code posted above, as ChaserNL said you'r missing the 'VALUES' keyword and he stated the right syntax. Also I noticed that all your variables are enclosed in single quotes like '" + info.id_emp + "', Single quotes are used only when sending any type of string or datetime like (Varchar, char, nvarchar, smalldatetime, datetime,...) so you should remove single quotes enclosing variables like (integer, number, double, money, bigint,...).

Hope this could help you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top