Esempio n. 1
0
        /// <summary>
        /// Build INSERT, UPDATE, DELETE queries
        /// </summary>
        /// <param name="dbParameters">Connection Parameters</param>
        /// <returns>Returns the number of rows affected if command succeeds. Returns -1 if command fails. Command execution success will also be stored in DatabaseConnectionParameters.LastCommandSucceeded</returns>
        public static int BuildCommand(DatabaseConnectionParameters dbParameters)
        {
            try
            {
                var odbcCommand = new OdbcCommand();
                odbcCommand.CommandText    = dbParameters.QueryString;
                odbcCommand.Connection     = dbParameters.ODBCConnection;
                odbcCommand.CommandType    = dbParameters.CommandType;
                odbcCommand.CommandTimeout = dbParameters.CommandTimeout;

                if (dbParameters.QueryParameters != null)
                {
                    foreach (OdbcParameter item in dbParameters.QueryParameters)
                    {
                        odbcCommand.Parameters.Add(item);
                    }
                }

                var odbcAdapter = new OdbcDataAdapter();

                switch (dbParameters.CommandBuildType)
                {
                case CommandBuildType.Insert:
                    odbcAdapter.InsertCommand = odbcCommand;
                    break;

                case CommandBuildType.Update:
                    odbcAdapter.UpdateCommand = odbcCommand;
                    break;

                case CommandBuildType.Delete:
                    odbcAdapter.DeleteCommand = odbcCommand;
                    break;

                default:
                    throw new Exception("Build Type missing");
                }

                dbParameters.LastCommandSucceeded = true;

                switch (dbParameters.DataContainerType)
                {
                case DataContainerType.DataTable:
                    return(odbcAdapter.Update(dbParameters.DataTableContainer));

                case DataContainerType.DataSet:
                    return(odbcAdapter.Update(dbParameters.DataSetContainer));

                case DataContainerType.DataSetWithTable:
                    return(odbcAdapter.Update(dbParameters.DataSetContainer, dbParameters.DataTableContainerName));

                case DataContainerType.DataRowsCollection:
                    return(odbcAdapter.Update(dbParameters.DataRowsCollectionContainer));

                default:
                    throw new Exception("Data Container Type missing");
                }
            }
            catch (Exception excp)
            {
                Logs.AddException(excp);
                dbParameters.LastCommandSucceeded = false;

                return(-1);
            }
        }