1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
public static int ExecuteInsertParamsQuery(string insertQuery, List<Param> parameters)
{
// SelectQuery
if (insertQuery.StringCounter("@") != parameters.Count)
{
throw new System.ArgumentException("Le nombre de paramètres déclarés ne correspond pas au nombre passé", "parameters");
}
// Objet connection
OleDbConnection connection = new OleDbConnection(Const._connectionString);
// Objet Command
OleDbCommand command = new OleDbCommand(insertQuery, connection);
// Params
foreach (Param param in parameters)
{
GetCommandParameter(command, param.Name, param.DataType, param.Value);
}
// Open
connection.Open();
// Inserted id
int idObject = command.ExecuteNonQuery();
// Close
connection.Close();
return idObject;
}
private static void GetCommandParameter(OleDbCommand command, string name, Type type, string value)
{
switch (type.Name)
{
case "String":
command.Parameters.Add(name, OleDbType.WChar, 255).Value = value;
break;
case "Int32":
command.Parameters.Add(name, OleDbType.Integer).Value = Convert.ToInt32(value);
break;
case "Boolean":
command.Parameters.Add(name, OleDbType.Boolean).Value = Convert.ToBoolean(value);
break;
default:
throw new System.ArgumentException("The specified type is unknow", "type");
}
} |
Partager