Ваш SQL-оператор неверен из-за отсутствия скобок для значений.
Код очень испорчен, и это трудно увидеть на первый взгляд. Так что лучше использовать параметры, чтобы иметь более чистое заявление вы можете легко прочитать и проверить наличие синтаксических ошибок:
INSERT INTO do_information
(die_class_code, subinventory_code, contact_code, company_code, corg_code, created_on, created_by)
VALUES
(@CodeId, @SubInventoryCode, @ContactCode, @CompanyCode, @CorgCode, GETDATE(), @UserCode)
Но вы можете сделать даже больше, чтобы получить этот код в чистоте. Оберните все ваши запросы. Вот пример для заявления:
Начиная с некоторых многоразовых базовых деклараций
public interface IExecuteQuery
{
int Execute();
Task<int> ExecuteAsync(CancellationToken cancellationToken);
}
public abstract class SqlExecuteQuery : IExecuteQuery
{
private readonly DbConnection _connection;
private readonly Lazy<DbCommand> _command;
protected SqlExecuteQuery(DbConnection connection)
{
if (connection == null)
throw new ArgumentNullException(nameof(connection));
_connection = connection;
_command = new Lazy<DbCommand>(
() =>
{
var command = _connection.CreateCommand();
PrepareCommand(command);
return command;
});
}
protected abstract void PrepareCommand(DbCommand command);
protected DbCommand Command => _command.Value;
protected virtual string GetParameterNameFromPropertyName(string propertyName)
{
return "@" + propertyName;
}
protected T GetParameterValue<T>([CallerMemberName] string propertyName = null)
{
object value = Command.Parameters[ GetParameterNameFromPropertyName(propertyName) ].Value;
if (value == DBNull.Value)
{
value = null;
}
return (T) value;
}
protected void SetParamaterValue<T>(T newValue, [CallerMemberName] string propertyName = null)
{
object value = newValue;
if (value == null)
{
value = DBNull.Value;
}
Command.Parameters[ GetParameterNameFromPropertyName(propertyName) ].Value = value;
}
protected virtual void OnBeforeExecute() { }
public int Execute()
{
OnBeforeExecute();
return Command.ExecuteNonQuery();
}
public async Task<int> ExecuteAsync(CancellationToken cancellationToken)
{
OnBeforeExecute();
return await Command.ExecuteNonQueryAsync(cancellationToken);
}
}
public static class DbCommandExtensions
{
public static DbParameter AddParameter(this DbCommand command, Action<DbParameter> configureAction)
{
var parameter = command.CreateParameter();
configureAction(parameter);
command.Parameters.Add(parameter);
return parameter;
}
}
Теперь определить интерфейс для вашего заявления
public interface IInsertInformationQuery : IExecuteQuery
{
string CodeId { get; set; }
string SubInventoryCode { get; set; }
string ContactCode { get; set; }
string CompanyCode { get; set; }
string CorgCode { get; set; }
string UserCode { get; }
}
Реализация
public class SqlInsertInformationQuery : SqlExecuteQuery, IInsertInformationQuery
{
public SqlInsertInformationQuery(DbConnection connection) : base(connection)
{
}
protected override void OnBeforeExecute()
{
UserCode = App_Common._USER_CODE; // this should be injected
}
protected override void PrepareCommand(DbCommand command)
{
command.CommandText =
@"INSERT INTO do_information (die_class_code, subinventory_code, contact_code, company_code, corg_code, created_on, created_by) " +
@"VALUES (@CodeId, @SubInventoryCode, @ContactCode, @CompanyCode, @CorgCode, GETDATE(), @UserCode)";
command.AddParameter(p =>
{
p.ParameterName = "@CodeId";
p.DbType = System.Data.DbType.String;
p.Direction = System.Data.ParameterDirection.Input;
});
command.AddParameter(p =>
{
p.ParameterName = "@SubInventoryCode";
p.DbType = System.Data.DbType.String;
p.Direction = System.Data.ParameterDirection.Input;
});
command.AddParameter(p =>
{
p.ParameterName = "@ContactCode";
p.DbType = System.Data.DbType.String;
p.Direction = System.Data.ParameterDirection.Input;
});
command.AddParameter(p =>
{
p.ParameterName = "@CompanyCode";
p.DbType = System.Data.DbType.String;
p.Direction = System.Data.ParameterDirection.Input;
});
command.AddParameter(p =>
{
p.ParameterName = "@CorgCode";
p.DbType = System.Data.DbType.String;
p.Direction = System.Data.ParameterDirection.Input;
});
command.AddParameter(p =>
{
p.ParameterName = "@UserCode";
p.DbType = System.Data.DbType.String;
p.Direction = System.Data.ParameterDirection.Input;
});
}
public string CodeId
{
get => GetParameterValue<string>();
set => SetParamaterValue(value);
}
public string SubInventoryCode
{
get => GetParameterValue<string>();
set => SetParamaterValue(value);
}
public string ContactCode
{
get => GetParameterValue<string>();
set => SetParamaterValue(value);
}
public string CompanyCode
{
get => GetParameterValue<string>();
set => SetParamaterValue(value);
}
public string CorgCode
{
get => GetParameterValue<string>();
set => SetParamaterValue(value);
}
public string UserCode
{
get => GetParameterValue<string>();
private set => SetParamaterValue(value);
}
}
Наконец ваш код будет выглядеть как
public override bool fnSaveNewRecord()
{
var database = new SqlDatabase(App_Common._WSFCSConnStr);
using (var connection = database.CreateConnection())
{
connection.Open();
IInsertInformationQuery query = new SqlInserInformationQuery(connection);
query.CodeId = txt_CodeID.Text.Trim();
query.SubInventoryCode = cbx_SubInventoryCode.Text;
query.ContactCode = cbx_ContactCode.Text;
query.CompanyCode = cbx_CompanyCode.Text;
query.CorgCode = cbx_CorgCode.Text;
var recordsAffected = query.Execute();
}
return base.fnSaveNewRecord();
}
Вам нужно использовать 'VALUES (v1, v2, ...)' - так с помощью скобок. –
Вы должны действительно использовать параметризованный запрос вместо объединения значений в строку запроса. Ваше текущее решение уязвимо для SQL-инъекций и плохой практики безопасности. –
Некоторые замечания OT: а) вам не нужно 'as SqlDatabase', поскольку вы уже создали его; b) вам не нужно '_ds = new DataSet();' поскольку вы будете * переписывать * этот пустой набор данных в своем вызове ExecuteDataSetQ; c) вы, похоже, не используете этот '_ds' (и вам не нужен он для вставки), существует ли простой метод Execute, который вы можете использовать? –