static internal Exception ParameterConversionFailed(object value, Type destType, Exception inner) { Debug.Assert(null != value, "null value on conversion failure"); Debug.Assert(null != inner, "null inner on conversion failure"); Exception e; string message = Res.GetString(Res.ADP_ParameterConversionFailed, value.GetType().Name, destType.Name); if (inner is ArgumentException) { e = new ArgumentException(message, inner); } else if (inner is FormatException) { e = new FormatException(message, inner); } else if (inner is InvalidCastException) { e = new InvalidCastException(message, inner); } else if (inner is OverflowException) { e = new OverflowException(message, inner); } else { e = inner; } return(e); }
// Copy the data from the Stream to the array buffer. // If the SqlBytes doesn't hold a buffer or the buffer // is not big enough, allocate new byte array. private void CopyStreamToBuffer() { Debug.Assert(FStream()); long lStreamLen = m_stream.Length; if (lStreamLen >= x_lMaxLen) { throw new SqlTypeException(Res.GetString(Res.SqlMisc_WriteOffsetLargerThanLenMessage)); } if (m_rgbBuf == null || m_rgbBuf.Length < lStreamLen) { m_rgbBuf = new byte[lStreamLen]; } if (m_stream.Position != 0) { m_stream.Seek(0, SeekOrigin.Begin); } m_stream.Read(m_rgbBuf, 0, (int)lStreamLen); m_stream = null; _lCurLen = lStreamLen; _state = SqlBytesCharsState.Buffer; AssertValid(); }
// Set the current length of the data // If the SqlBytes is Null, setLength will make it non-Null. public void SetLength(long value) { if (value < 0) { throw new ArgumentOutOfRangeException("value"); } if (FStream()) { m_stream.SetLength(value); } else { // If there is a buffer, even the value of SqlBytes is Null, // still allow setting length to zero, which will make it not Null. // If the buffer is null, raise exception // if (null == m_rgbBuf) { throw new SqlTypeException(Res.GetString(Res.SqlMisc_NoBufferMessage)); } if (value > (long)m_rgbBuf.Length) { throw new ArgumentOutOfRangeException("value"); } else if (IsNull) { // At this point we know that value is small enough // Go back in buffer mode _state = SqlBytesCharsState.Buffer; } _lCurLen = value; } AssertValid(); }
private const int const_ErrorMessageBufferSize = 1024; // Buffer size for Local DB error message 1K will be enough for all messages internal static string GetLocalDBMessage(int hrCode) { Debug.Assert(hrCode < 0, "HRCode does not indicate error"); try { StringBuilder buffer = new StringBuilder((int)const_ErrorMessageBufferSize); UInt32 len = (UInt32)buffer.Capacity; // First try for current culture int hResult = LocalDBFormatMessage(hrLocalDB: hrCode, dwFlags: const_LOCALDB_TRUNCATE_ERR_MESSAGE, dwLanguageId: (uint)Locale.GetCurrentCultureLcid(), buffer: buffer, buflen: ref len); if (hResult >= 0) { return(buffer.ToString()); } else { // Message is not available for current culture, try default buffer = new StringBuilder((int)const_ErrorMessageBufferSize); len = (UInt32)buffer.Capacity; hResult = LocalDBFormatMessage(hrLocalDB: hrCode, dwFlags: const_LOCALDB_TRUNCATE_ERR_MESSAGE, dwLanguageId: 0 /* thread locale with fallback to English */, buffer: buffer, buflen: ref len); if (hResult >= 0) { return(buffer.ToString()); } else { return(string.Format(CultureInfo.CurrentCulture, "{0} (0x{1:X}).", Res.GetString("LocalDB_UnobtainableMessage"), hResult)); } } } catch (SqlException exc) { return(string.Format(CultureInfo.CurrentCulture, "{0} ({1}).", Res.GetString("LocalDB_UnobtainableMessage"), exc.Message)); } }
static private string ConnectionStateMsg(ConnectionState state) { switch (state) { case (ConnectionState.Closed): case (ConnectionState.Connecting | ConnectionState.Broken): return(Res.GetString(Res.ADP_ConnectionStateMsg_Closed)); case (ConnectionState.Connecting): return(Res.GetString(Res.ADP_ConnectionStateMsg_Connecting)); case (ConnectionState.Open): return(Res.GetString(Res.ADP_ConnectionStateMsg_Open)); case (ConnectionState.Open | ConnectionState.Executing): return(Res.GetString(Res.ADP_ConnectionStateMsg_OpenExecuting)); case (ConnectionState.Open | ConnectionState.Fetching): return(Res.GetString(Res.ADP_ConnectionStateMsg_OpenFetching)); default: return(Res.GetString(Res.ADP_ConnectionStateMsg, state.ToString())); } }
static internal ArgumentException InvalidSizeValue(int value) { return(Argument(Res.GetString(Res.ADP_InvalidSizeValue, value.ToString(CultureInfo.InvariantCulture)))); }
static internal ArgumentException DbTypeNotSupported(System.Data.DbType type, Type enumtype) { return(Argument(Res.GetString(Res.ADP_DbTypeNotSupported, type.ToString(), enumtype.Name))); }
static internal Exception EmptyDatabaseName() { return(Argument(Res.GetString(Res.ADP_EmptyDatabaseName))); }
// Write data of specified length into the SqlBytes from specified offset public void Write(long offset, byte[] buffer, int offsetInBuffer, int count) { if (FStream()) { if (m_stream.Position != offset) { m_stream.Seek(offset, SeekOrigin.Begin); } m_stream.Write(buffer, offsetInBuffer, count); } else { // Validate the arguments if (buffer == null) { throw new ArgumentNullException("buffer"); } if (m_rgbBuf == null) { throw new SqlTypeException(Res.GetString(Res.SqlMisc_NoBufferMessage)); } if (offset < 0) { throw new ArgumentOutOfRangeException("offset"); } if (offset > m_rgbBuf.Length) { throw new SqlTypeException(Res.GetString(Res.SqlMisc_BufferInsufficientMessage)); } if (offsetInBuffer < 0 || offsetInBuffer > buffer.Length) { throw new ArgumentOutOfRangeException("offsetInBuffer"); } if (count < 0 || count > buffer.Length - offsetInBuffer) { throw new ArgumentOutOfRangeException("count"); } if (count > m_rgbBuf.Length - offset) { throw new SqlTypeException(Res.GetString(Res.SqlMisc_BufferInsufficientMessage)); } if (IsNull) { // If NULL and there is buffer inside, we only allow writing from // offset zero. // if (offset != 0) { throw new SqlTypeException(Res.GetString(Res.SqlMisc_WriteNonZeroOffsetOnNullMessage)); } // treat as if our current length is zero. // Note this has to be done after all inputs are validated, so that // we won't throw exception after this point. // _lCurLen = 0; _state = SqlBytesCharsState.Buffer; } else if (offset > _lCurLen) { // Don't allow writing from an offset that this larger than current length. // It would leave uninitialized data in the buffer. // throw new SqlTypeException(Res.GetString(Res.SqlMisc_WriteOffsetLargerThanLenMessage)); } if (count != 0) { // ProjectK\Core doesn't support long-typed array indexers Debug.Assert(offset < int.MaxValue); Array.Copy(buffer, offsetInBuffer, m_rgbBuf, checked ((int)offset), count); // If the last position that has been written is after // the current data length, reset the length if (_lCurLen < offset + count) { _lCurLen = offset + count; } } } AssertValid(); }
public SqlTypeException() : this(Res.GetString(Res.SqlMisc_SqlTypeMessage), null) { }
// // : IDbTransaction // static internal Exception ParallelTransactionsNotSupported(DbConnection obj) { return(InvalidOperation(Res.GetString(Res.ADP_ParallelTransactionsNotSupported, obj.GetType().Name))); }
static internal ArgumentException VersionDoesNotSupportDataType(string typeName) { return(Argument(Res.GetString(Res.ADP_VersionDoesNotSupportDataType, typeName))); }
static internal Exception InvalidDataLength(long length) { return(IndexOutOfRange(Res.GetString(Res.SQL_InvalidDataLength, length.ToString(CultureInfo.InvariantCulture)))); }
static internal IndexOutOfRangeException InvalidBufferSizeOrIndex(int numBytes, int bufferIndex) { return(IndexOutOfRange(Res.GetString(Res.SQL_InvalidBufferSizeOrIndex, numBytes.ToString(CultureInfo.InvariantCulture), bufferIndex.ToString(CultureInfo.InvariantCulture)))); }
static internal ArgumentOutOfRangeException InvalidDestinationBufferIndex(int maxLen, int dstOffset, string parameterName) { return(ArgumentOutOfRange(Res.GetString(Res.ADP_InvalidDestinationBufferIndex, maxLen.ToString(CultureInfo.InvariantCulture), dstOffset.ToString(CultureInfo.InvariantCulture)), parameterName)); }
// // : DbDataReader // static internal Exception DataReaderClosed(string method) { return(InvalidOperation(Res.GetString(Res.ADP_DataReaderClosed, method))); }
static internal Exception InvalidConnectRetryIntervalValue() { return(Argument(Res.GetString(Res.SQLCR_InvalidConnectRetryIntervalValue))); }
static internal Exception InternalError(InternalErrorCode internalError) { return(InvalidOperation(Res.GetString(Res.ADP_InternalProviderError, (int)internalError))); }
static internal Exception InternalConnectionError(ConnectionError internalError) { return(InvalidOperation(Res.GetString(Res.ADP_InternalConnectionError, (int)internalError))); }
static internal ArgumentException ParameterValueOutOfRange(Decimal value) { return(ADP.Argument(Res.GetString(Res.ADP_ParameterValueOutOfRange, value.ToString((IFormatProvider)null)))); }
static internal ArgumentException ParameterValueOutOfRange(SqlDecimal value) { return(ADP.Argument(Res.GetString(Res.ADP_ParameterValueOutOfRange, value.ToString()))); }
static internal InvalidOperationException AsyncOperationPending() { return(InvalidOperation(Res.GetString(Res.ADP_PendingAsyncOperation))); }
static internal Exception ConnectionAlreadyOpen(ConnectionState state) { return(InvalidOperation(Res.GetString(Res.ADP_ConnectionAlreadyOpen, ADP.ConnectionStateMsg(state)))); }
// // : Stream // static internal Exception StreamClosed(string method) { return(InvalidOperation(Res.GetString(Res.ADP_StreamClosed, method))); }
static internal Exception TransactionZombied(DbTransaction obj) { return(InvalidOperation(Res.GetString(Res.ADP_TransactionZombied, obj.GetType().Name))); }
static internal ArgumentException InvalidDataType(string typeName) { return(Argument(Res.GetString(Res.ADP_InvalidDataType, typeName))); }
static internal Exception ClosedConnectionError() { return(InvalidOperation(Res.GetString(Res.ADP_ClosedConnectionError))); }
static internal ArgumentException UnknownDataType(Type dataType) { return(Argument(Res.GetString(Res.ADP_UnknownDataType, dataType.FullName))); }
static internal IOException ErrorReadingFromStream(Exception internalException) { return(IO(Res.GetString(Res.SqlMisc_StreamErrorMessage), internalException)); }
static internal Exception OpenConnectionPropertySet(string property, ConnectionState state) { return(InvalidOperation(Res.GetString(Res.ADP_OpenConnectionPropertySet, property, ADP.ConnectionStateMsg(state)))); }