public SqlParameter CreateParamater(string name, System.Data.DbType type, int size, System.Data.ParameterDirection direction) { return(new SqlParameter { ParameterName = name, DbType = type, Size = size, Direction = direction }); }
/// <summary> /// Initializes a new instance of the ColumnEnumAttribute class. /// </summary> /// <param name="name"></param> /// <param name="systemType"></param> /// <param name="dbType"></param> /// <param name="isPrimaryKey"></param> /// <param name="isIdentity"></param> /// <param name="allowDbNull"></param> /// <param name="length"></param> public ColumnEnumAttribute(String name, Type systemType, System.Data.DbType dbType, bool isPrimaryKey, bool isIdentity, bool allowDbNull, int length) { this.Name = name; this.SystemType = systemType; this.DbType = dbType; this.IsPrimaryKey = isPrimaryKey; this.IsIdentity = isIdentity; this.AllowDbNull = allowDbNull; this.Length = length; }
/// <summary> /// Creates a new DBParam with the specified name, DBType, size and direction. Cannot support delegate methods as values /// </summary> /// <param name="genericName">The generic name of the parameter (e.g. param1 rather than @param1)</param> /// <param name="type">The DbType of the parameter</param> /// <param name="size">The (maximum) size of the parameter value</param> /// <param name="direction">The parameters direction</param> /// <returns></returns> public static DBParam Param(string genericName, System.Data.DbType type, int size, System.Data.ParameterDirection direction) { DBParam pref = new DBParamRef(); pref.Name = genericName; pref.DbType = type; pref.Size = size; pref.Direction = direction; return(pref); }
/// <summary> /// Creates a new DBParam with the specified name, type, length and value /// </summary> /// <param name="genericName"></param> /// <param name="type"></param> /// <param name="length"></param> /// <param name="value"></param> /// <returns></returns> public static DBParam ParamWithValue(string genericName, System.Data.DbType type, int length, object value) { DBParam pref = new DBParamRef(); pref.Name = genericName; pref.DbType = type; pref.Value = value; pref.Size = length; return(pref); }
/// <summary> /// [覆盖添加]添加参数 /// </summary> /// <param name="ParameterName"></param> /// <param name="Value"></param> /// <param name="DbType"></param> /// <param name="Size"></param> public void AddParameter(string ParameterName, object Value, System.Data.DbType DbType, int Size) { DbParameter DbParam = Command.CreateParameter(); DbParam.ParameterName = ParameterName; DbParam.DbType = DbType; DbParam.Size = Size; DbParam.Value = Value; AddParameter(DbParam); }
public System.Data.Common.DbParameter CreateParameter(string parameterName, System.Data.DbType dbType, int size, object value) { var parameter = CreateParameter(); parameter.ParameterName = parameterName; parameter.Value = value; parameter.DbType = dbType; parameter.Size = size; return(parameter); }
private static System.Data.SqlDbType DbTypeToSqlDbType(System.Data.DbType pSourceType) { if (pSourceType == System.Data.DbType.Object) { return(System.Data.SqlDbType.Text); } System.Data.SqlClient.SqlParameter paraConver = new System.Data.SqlClient.SqlParameter(); paraConver.DbType = pSourceType; return(paraConver.SqlDbType); }
public static void InitializeInsertCommand <T>(SQLiteCommand cmd) { cmd.Parameters.Clear(); var insertColumns = new List <string>(); var insertParameters = new List <string>(); foreach (var prop in typeof(T).GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)) { if (prop.GetMethod.IsPrivate) { continue; } var ignoreAttr = prop.GetCustomAttribute <System.ComponentModel.DataAnnotations.Schema.NotMappedAttribute>(); if (ignoreAttr != null) { continue; } var dbGenAttr = prop.GetCustomAttribute <System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedAttribute>(); if (dbGenAttr != null && dbGenAttr.DatabaseGeneratedOption != System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.None) { continue; } var fkAttr = prop.GetCustomAttribute <System.ComponentModel.DataAnnotations.Schema.ForeignKeyAttribute>(); if (fkAttr != null) { continue; } var columnAttr = prop.GetCustomAttribute <System.ComponentModel.DataAnnotations.Schema.ColumnAttribute>(); string columnName = columnAttr?.Name ?? prop.Name; insertColumns.Add(columnName); insertParameters.Add("$" + prop.Name); System.Data.DbType paramType = System.Data.DbType.String; if (prop.PropertyType.Name.ToLower().Contains("int")) { paramType = System.Data.DbType.Int32; } else if (prop.PropertyType.Name.ToLower().Contains("bool")) { paramType = System.Data.DbType.Boolean; } cmd.Parameters.Add("$" + prop.Name, paramType); } cmd.CommandText = $"INSERT INTO {GetTableName<T>()} ({string.Join(",", insertColumns)}) VALUES ({string.Join(",", insertParameters)})"; }
protected MetadataEntityProperty(IEntityMetadata entity, string name, System.Data.DbType type) { if (string.IsNullOrEmpty(name)) { throw new ArgumentNullException(nameof(name)); } _entity = entity ?? throw new ArgumentNullException(nameof(entity)); _name = name.Trim(); _type = type; }
public FieldDefinition(string name, System.Data.DbType type, bool nullable = true) { if (string.IsNullOrEmpty(name)) { throw new ArgumentNullException(nameof(name)); } this.Name = name; this.DbType = type; this.Nullable = nullable; }
private void AddWhereParameters(string Name, System.Data.DbType t, string val) { foreach (Parameter p in WhereParameters) { if (p.Name == Name) { return; } } WhereParameters.Add(Name, t, val); }
public bool SetDataType(string parameterName, System.Data.DbType type) { for (int i = 0; i < Parameters.Count; i++) { if (Parameters[i].ParameterName == parameterName) { Parameters[i].DbType = type; return(true); } } return(false); }
/// <summary> /// Creates a new DBParam with a delegate method as the value provider (no parameters, returns object). /// </summary> /// <param name="type">The DbType of the parameter </param> /// <param name="valueProvider">The delegate method (cannot be null)</param> /// <returns></returns> /// <remarks> /// The valueProvider method will be executed when any queries using this parameter are converted to their SQL statements /// </remarks> public static DBParam ParamWithDelegate(System.Data.DbType type, ParamValue valueProvider) { if (null == valueProvider) { throw new ArgumentNullException("valueProvider"); } DBDelegateParam del = new DBDelegateParam(valueProvider); del.DbType = type; return(del); }
public static System.Data.DataTable dtParams() { System.Data.DbType tipo = new System.Data.DbType(); System.Data.ParameterDirection direccion = new System.Data.ParameterDirection(); System.Data.DataTable dt = new System.Data.DataTable(); dt.Columns.Add("Nombre", System.Type.GetType("System.String")); dt.Columns.Add("Tipo", tipo.GetType()); dt.Columns.Add("Size", System.Type.GetType("System.Int32")); dt.Columns.Add("Direccion", direccion.GetType()); dt.Columns.Add("Valor", System.Type.GetType("System.Object")); return(dt); }
} // End Function AddParameter public virtual System.Data.Common.DbParameter AddParameter(System.Data.Common.DbCommand command, string strParameterName, object objValue, System.Data.ParameterDirection pad) { if (objValue == null) { //throw new ArgumentNullException("objValue"); objValue = System.DBNull.Value; } // End if (objValue == null) System.Type tDataType = objValue.GetType(); System.Data.DbType dbType = GetDbType(tDataType); return(AddParameter(command, strParameterName, objValue, pad, dbType)); } // End Function AddParameter
public static System.Data.IDbDataParameter AddParameter(System.Data.IDbCommand command, string strParameterName, object objValue, System.Data.ParameterDirection pad) { if (objValue == null) { objValue = System.DBNull.Value; } System.Type tDataType = objValue.GetType(); System.Data.DbType dbType = GetDbType(tDataType); return(AddParameter(command, strParameterName, objValue, pad, dbType)); }
/// <summary> /// Creates a new DBParam with the specified name, type, size and a delegate method as the value provider (no parameters, returns object). /// </summary> /// <param name="genericName">The generic name of the parameter (e.g. param1 rather than @param1)</param> /// <param name="type">The DbType of the parameter</param> /// <param name="size">The maximum size of the parameter value</param> /// <param name="valueprovider">The delegate method (cannot be null)</param> /// <returns></returns> /// <remarks> /// The method will be executed when any queries using this parameter are converted to their SQL statements /// </remarks> public static DBParam ParamWithDelegate(string genericName, System.Data.DbType type, int size, ParamValue valueprovider) { if (null == valueprovider) { throw new ArgumentNullException("valueProvider"); } DBDelegateParam del = new DBDelegateParam(valueprovider); del.DbType = type; del.Size = size; del.Name = genericName; return(del); }
/// <summary> /// Add the parameter to the command. /// </summary> /// <param name="command">the command where the parameter will be added.</param> /// <param name="name">The name of parameter.</param> /// <param name="type">The type of parameter.</param> /// <param name="value">The value of the parameter.</param> /// <param name="size">The size of the parameter.</param> /// <returns></returns> public static ICommand Param(this ICommand command, string name, System.Data.DbType type, object value, int size = 0) { if (command is BaseStatement) { if (value != null) { (command as BaseStatement).AddParameter(name, type, value, size); } else { (command as BaseStatement).AddParameter(name, type, DBNull.Value, size); } } return(command); }
public System.Data.DbType[] GetMappingColumnTypes(string[] propertyNames) { if (propertyNames == null) { return(null); } System.Data.DbType[] mappingColumnTypes = new System.Data.DbType[propertyNames.Length]; for (int i = 0; i < propertyNames.Length; i++) { mappingColumnTypes[i] = GetPropertyConfiguration(propertyNames[i]).DbType; } return(mappingColumnTypes); }
/// <summary> /// Creates a new SqlParameter, optionally specifying the dbtype. /// </summary> /// <param name="value"></param> /// <param name="dbType"></param> /// <param name="size"></param> /// <returns></returns> public string CreateParameter(object value, System.Data.DbType dbType, int?size = null) { _paramIndex++; var p = new DbParameterValue() { ParameterName = string.Concat("p", this._paramIndex.ToString()), Value = util.ConvertValue(value, dbType), DbType = dbType, Size = size }; this.Parameters.Add(p); return(p.ParameterName); }
public static DbParameter MakeParameter(string Name, object value, System.Data.DbType Type) { switch (DatabaseSettings.DbType) { case Enums.DatabaseType.MsSQL: return(new SqlParameter(Name, Type) { Value = value }); case Enums.DatabaseType.MySQL: MySqlParameter p = new MySqlParameter(Name, value); switch (Type) { case System.Data.DbType.String: p.MySqlDbType = MySqlDbType.Text; break; case System.Data.DbType.DateTime: p.MySqlDbType = MySqlDbType.DateTime; break; case System.Data.DbType.Int32: p.MySqlDbType = MySqlDbType.Int32; break; case System.Data.DbType.AnsiString: p.MySqlDbType = MySqlDbType.Text; break; case System.Data.DbType.Boolean: p.MySqlDbType = MySqlDbType.Bit; break; case System.Data.DbType.Int64: p.MySqlDbType = MySqlDbType.Int64; break; default: break; } return(p); default: return(null); } }
/// <summary> /// 通过制定属性的索引号,设置属性值 /// </summary> /// <param name="propertyFieldName">属性字段名</param> /// <param name="propertyIndex">属性字段的索引位置</param> /// <param name="Value">字符串值</param> /// <param name="maxLength">字符长度</param> protected internal void setProperty(string propertyFieldName, int propertyIndex, string Value, int maxLength) { System.Data.DbType dbType = System.Data.DbType.String; if (maxLength <= 0) { dbType = System.Data.DbType.AnsiString; } if (Value != null && maxLength > 0 && Value.Length > maxLength) { throw new Exception("字段" + propertyFieldName + "的实际长度超出了最大长度" + maxLength); } else { setPropertyValueAndLength(propertyFieldName, propertyIndex, Value, maxLength, dbType); } }
static public Dictionary <string, SQLiteParameter> GetParameters(Guid dataID, tgProviderSpecificMetadata providerMetadata, tgColumnMetadataCollection columns) { lock (parameterCache) { if (!parameterCache.ContainsKey(dataID)) { // The Parameters for this Table haven't been cached yet, this is a one time operation Dictionary <string, SQLiteParameter> types = new Dictionary <string, SQLiteParameter>(); SQLiteParameter param1; foreach (tgColumnMetadata col in columns) { tgTypeMap typeMap = providerMetadata.GetTypeMap(col.PropertyName); if (typeMap != null) { string nativeType = typeMap.NativeType; System.Data.DbType dbType = Cache.NativeTypeToDbType(nativeType); param1 = new SQLiteParameter(Delimiters.Param + col.PropertyName, dbType, 0, col.Name); param1.SourceColumn = col.Name; switch (dbType) { case System.Data.DbType.Int64: //case VistaDBType.Int: //case VistaDBType.SmallInt: //case VistaDBType.Decimal: //case VistaDBType.Float: //case VistaDBType.Money: //case VistaDBType.SmallMoney: param1.Size = (int)col.CharacterMaxLength; break; } types[col.Name] = param1; } } parameterCache[dataID] = types; } } return(parameterCache[dataID]); }
public override void WriteLiteral(System.Data.DbType dbType, object value, bool isInorNot = false) { if (dbType == System.Data.DbType.Time) { WriteLiteralToDate(value, "HH:mm:ss", "HH24:MI:SS"); } else if (dbType == System.Data.DbType.Date) { WriteLiteralToDate(value, "yyyy-MM-dd", "YYYY-MM-DD"); } else if (dbType == System.Data.DbType.DateTime || dbType == System.Data.DbType.DateTime2) { WriteLiteralToDate(value, "yyyy-MM-dd HH:mm:ss", "YYYY-MM-DD HH24:MI:SS"); } else { base.WriteLiteral(dbType, value); } }
public static Type ToType(this System.Data.DbType dbType) { var typeMap = new Dictionary <System.Data.DbType, Type> { [System.Data.DbType.Byte] = typeof(byte), [System.Data.DbType.SByte] = typeof(sbyte), [System.Data.DbType.Int16] = typeof(short), [System.Data.DbType.UInt16] = typeof(ushort), [System.Data.DbType.Int32] = typeof(int), [System.Data.DbType.UInt32] = typeof(uint), [System.Data.DbType.Int64] = typeof(long), [System.Data.DbType.UInt64] = typeof(ulong), [System.Data.DbType.Single] = typeof(float), [System.Data.DbType.Double] = typeof(double), [System.Data.DbType.Decimal] = typeof(decimal), [System.Data.DbType.Boolean] = typeof(bool), [System.Data.DbType.String] = typeof(string), [System.Data.DbType.StringFixedLength] = typeof(char), [System.Data.DbType.Guid] = typeof(Guid), [System.Data.DbType.DateTime] = typeof(DateTime), [System.Data.DbType.DateTimeOffset] = typeof(DateTimeOffset), [System.Data.DbType.Binary] = typeof(byte[]), [System.Data.DbType.Byte] = typeof(byte?), [System.Data.DbType.SByte] = typeof(sbyte?), [System.Data.DbType.Int16] = typeof(short?), [System.Data.DbType.UInt16] = typeof(ushort?), [System.Data.DbType.Int32] = typeof(int?), [System.Data.DbType.UInt32] = typeof(uint?), [System.Data.DbType.Int64] = typeof(long?), [System.Data.DbType.UInt64] = typeof(ulong?), [System.Data.DbType.Single] = typeof(float?), [System.Data.DbType.Double] = typeof(double?), [System.Data.DbType.Decimal] = typeof(decimal?), [System.Data.DbType.Boolean] = typeof(bool?), [System.Data.DbType.StringFixedLength] = typeof(char?), [System.Data.DbType.Guid] = typeof(Guid?), [System.Data.DbType.DateTime] = typeof(DateTime?), [System.Data.DbType.DateTimeOffset] = typeof(DateTimeOffset?) //,[System.Data.DbType.Binary] = typeof(System.Data.Linq.Binary) }; return(typeMap[dbType]); }
public void setAttribute(string name, System.Reflection.CustomAttributeTypedArgument attr) { if (attr.ArgumentType.Equals(typeof(AccessTypes))) { AccessType = (AccessTypes)attr.Value; } else if (attr.ArgumentType.Equals(typeof(ColumnTypes))) { ColumnType = (ColumnTypes)attr.Value; } else if (attr.ArgumentType.Equals(typeof(System.Data.DbType))) { DbType = (System.Data.DbType)attr.Value; } else if (name == "ColumnName") { Name = (string)attr.Value; } }
private static System.Data.DbType GetDbType(object obj) { if (obj == null) { return(System.Data.DbType.Object); } System.Type type = obj.GetType(); // http://social.msdn.microsoft.com/Forums/en/winforms/thread/c6f3ab91-2198-402a-9a18-66ce442333a6 string strTypeName = type.Name; System.Data.DbType dbType = System.Data.DbType.String; // default value try { if (object.ReferenceEquals(type, typeof(System.DBNull))) { return(dbType); } if (object.ReferenceEquals(type, typeof(System.Byte[]))) { return(System.Data.DbType.Binary); } dbType = (System.Data.DbType)System.Enum.Parse(typeof(System.Data.DbType), strTypeName, true); // Es ist keine Zuordnung von DbType UInt64 zu einem bekannten SqlDbType vorhanden. // http://msdn.microsoft.com/en-us/library/bbw6zyha(v=vs.71).aspx if (dbType == System.Data.DbType.UInt64) { dbType = System.Data.DbType.Int64; } } catch (System.Exception) { // add error handling to suit your taste } return(dbType); } // End Function GetDbType
protected override string GetNativeTypeForDbType(System.Data.DbType dbType, int setSize, int accuracy, DBColumnFlags flags, out string options) { switch (dbType) { case System.Data.DbType.Guid: options = ""; return(" VARCHAR(50)"); case System.Data.DbType.Int16: case System.Data.DbType.Int32: case System.Data.DbType.Int64: case System.Data.DbType.UInt16: case System.Data.DbType.UInt32: case System.Data.DbType.UInt64: options = string.Empty; return(" INTEGER"); default: return(base.GetNativeTypeForDbType(dbType, setSize, accuracy, flags, out options)); } }
protected static System.Data.DbType GetDbType(System.Type type) { // http://social.msdn.microsoft.com/Forums/en/winforms/thread/c6f3ab91-2198-402a-9a18-66ce442333a6 string strTypeName = type.Name; System.Data.DbType DBtype = System.Data.DbType.String; // default value if (object.ReferenceEquals(type, typeof(System.DBNull))) { return(DBtype); } try { DBtype = (System.Data.DbType)System.Enum.Parse(typeof(System.Data.DbType), strTypeName, true); } catch { } return(DBtype); }
public string GetDbType(System.Data.DbType dbType, int length, byte precision, byte scale) { return(dbType.ToString()); }
public static HsqlProviderType ToHsqlProviderType(DbType type) { switch (type) { case DbType.AnsiString: { return HsqlProviderType.VarChar; } case DbType.AnsiStringFixedLength: { return HsqlProviderType.Char; } case DbType.Binary: { return HsqlProviderType.Binary; } case DbType.Boolean: { return HsqlProviderType.Boolean; } case DbType.Byte: { return HsqlProviderType.SmallInt; } case DbType.Currency: { return HsqlProviderType.Decimal; } case DbType.Date: { return HsqlProviderType.TimeStamp; } case DbType.DateTime: { return HsqlProviderType.TimeStamp; } case DbType.DateTime2: { return HsqlProviderType.TimeStamp; } case DbType.DateTimeOffset: { return HsqlProviderType.Char; } case DbType.Decimal: { return HsqlProviderType.Decimal; } case DbType.Double: { return HsqlProviderType.Double; } case DbType.Guid: { return HsqlProviderType.Binary; } case DbType.Int16: { return HsqlProviderType.SmallInt; } case DbType.Int32: { return HsqlProviderType.Integer; } case DbType.Int64: { return HsqlProviderType.BigInt; } case DbType.Object: { return HsqlProviderType.Object; } case DbType.SByte: { return HsqlProviderType.TinyInt; } case DbType.Single: { return HsqlProviderType.Real; } case DbType.String: { return HsqlProviderType.VarChar; } case DbType.StringFixedLength: { return HsqlProviderType.Char; } case DbType.Time: { return HsqlProviderType.TimeStamp; } case DbType.UInt16: { return HsqlProviderType.Integer; } case DbType.UInt32: { return HsqlProviderType.BigInt; } case DbType.UInt64: { return HsqlProviderType.Numeric; } case DbType.VarNumeric: { return HsqlProviderType.Numeric; } case DbType.Xml: { return HsqlProviderType.Xml; } default: { return HsqlProviderType.Object; } } }