本文整理汇总了C#中System.Data.SqlClient.TdsParserStateObject.WriteByteArray方法的典型用法代码示例。如果您正苦于以下问题:C# TdsParserStateObject.WriteByteArray方法的具体用法?C# TdsParserStateObject.WriteByteArray怎么用?C# TdsParserStateObject.WriteByteArray使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Data.SqlClient.TdsParserStateObject
的用法示例。
在下文中一共展示了TdsParserStateObject.WriteByteArray方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: WriteUnterminatedSqlValue
// For MAX types, this method can only write everything in one big chunk. If multiple
// chunk writes needed, please use WritePlpBytes/WritePlpChars
private Task WriteUnterminatedSqlValue(object value, MetaType type, int actualLength, int codePageByteSize, int offset, TdsParserStateObject stateObj)
{
Debug.Assert(((type.NullableType == TdsEnums.SQLXMLTYPE) ||
(value is INullable && !((INullable)value).IsNull)),
"unexpected null SqlType!");
// parameters are always sent over as BIG or N types
switch (type.NullableType)
{
case TdsEnums.SQLFLTN:
if (type.FixedLength == 4)
WriteFloat(((SqlSingle)value).Value, stateObj);
else
{
Debug.Assert(type.FixedLength == 8, "Invalid length for SqlDouble type!");
WriteDouble(((SqlDouble)value).Value, stateObj);
}
break;
case TdsEnums.SQLBIGBINARY:
case TdsEnums.SQLBIGVARBINARY:
case TdsEnums.SQLIMAGE:
{
if (type.IsPlp)
{
WriteInt(actualLength, stateObj); // chunk length
}
if (value is SqlBinary)
{
return stateObj.WriteByteArray(((SqlBinary)value).Value, actualLength, offset, canAccumulate: false);
}
else
{
Debug.Assert(value is SqlBytes);
return stateObj.WriteByteArray(((SqlBytes)value).Value, actualLength, offset, canAccumulate: false);
}
}
case TdsEnums.SQLUNIQUEID:
{
byte[] b = ((SqlGuid)value).ToByteArray();
Debug.Assert((actualLength == b.Length) && (actualLength == 16), "Invalid length for guid type in com+ object");
stateObj.WriteByteArray(b, actualLength, 0);
break;
}
case TdsEnums.SQLBITN:
{
Debug.Assert(type.FixedLength == 1, "Invalid length for SqlBoolean type");
if (((SqlBoolean)value).Value == true)
stateObj.WriteByte(1);
else
stateObj.WriteByte(0);
break;
}
case TdsEnums.SQLINTN:
if (type.FixedLength == 1)
stateObj.WriteByte(((SqlByte)value).Value);
else
if (type.FixedLength == 2)
WriteShort(((SqlInt16)value).Value, stateObj);
else
if (type.FixedLength == 4)
WriteInt(((SqlInt32)value).Value, stateObj);
else
{
Debug.Assert(type.FixedLength == 8, "invalid length for SqlIntN type: " + type.FixedLength.ToString(CultureInfo.InvariantCulture));
WriteLong(((SqlInt64)value).Value, stateObj);
}
break;
case TdsEnums.SQLBIGCHAR:
case TdsEnums.SQLBIGVARCHAR:
case TdsEnums.SQLTEXT:
if (type.IsPlp)
{
WriteInt(codePageByteSize, stateObj); // chunk length
}
if (value is SqlChars)
{
String sch = new String(((SqlChars)value).Value);
return WriteEncodingChar(sch, actualLength, offset, _defaultEncoding, stateObj, canAccumulate: false);
}
else
{
Debug.Assert(value is SqlString);
return WriteEncodingChar(((SqlString)value).Value, actualLength, offset, _defaultEncoding, stateObj, canAccumulate: false);
}
case TdsEnums.SQLNCHAR:
//.........这里部分代码省略.........
示例2: WriteUnterminatedValue
// For MAX types, this method can only write everything in one big chunk. If multiple
// chunk writes needed, please use WritePlpBytes/WritePlpChars
private Task WriteUnterminatedValue(object value, MetaType type, byte scale, int actualLength, int encodingByteSize, int offset, TdsParserStateObject stateObj, int paramSize, bool isDataFeed)
{
Debug.Assert((null != value) && (DBNull.Value != value), "unexpected missing or empty object");
// parameters are always sent over as BIG or N types
switch (type.NullableType)
{
case TdsEnums.SQLFLTN:
if (type.FixedLength == 4)
WriteFloat((Single)value, stateObj);
else
{
Debug.Assert(type.FixedLength == 8, "Invalid length for SqlDouble type!");
WriteDouble((Double)value, stateObj);
}
break;
case TdsEnums.SQLBIGBINARY:
case TdsEnums.SQLBIGVARBINARY:
case TdsEnums.SQLIMAGE:
case TdsEnums.SQLUDT:
{
// An array should be in the object
Debug.Assert(isDataFeed || value is byte[], "Value should be an array of bytes");
Debug.Assert(!isDataFeed || value is StreamDataFeed, "Value should be a stream");
if (isDataFeed)
{
Debug.Assert(type.IsPlp, "Stream assigned to non-PLP was not converted!");
return NullIfCompletedWriteTask(WriteStreamFeed((StreamDataFeed)value, stateObj, paramSize));
}
else
{
if (type.IsPlp)
{
WriteInt(actualLength, stateObj); // chunk length
}
return stateObj.WriteByteArray((byte[])value, actualLength, offset, canAccumulate: false);
}
}
case TdsEnums.SQLUNIQUEID:
{
System.Guid guid = (System.Guid)value;
byte[] b = guid.ToByteArray();
Debug.Assert((actualLength == b.Length) && (actualLength == 16), "Invalid length for guid type in com+ object");
stateObj.WriteByteArray(b, actualLength, 0);
break;
}
case TdsEnums.SQLBITN:
{
Debug.Assert(type.FixedLength == 1, "Invalid length for SqlBoolean type");
if ((bool)value == true)
stateObj.WriteByte(1);
else
stateObj.WriteByte(0);
break;
}
case TdsEnums.SQLINTN:
if (type.FixedLength == 1)
stateObj.WriteByte((byte)value);
else if (type.FixedLength == 2)
WriteShort((Int16)value, stateObj);
else if (type.FixedLength == 4)
WriteInt((Int32)value, stateObj);
else
{
Debug.Assert(type.FixedLength == 8, "invalid length for SqlIntN type: " + type.FixedLength.ToString(CultureInfo.InvariantCulture));
WriteLong((Int64)value, stateObj);
}
break;
case TdsEnums.SQLBIGCHAR:
case TdsEnums.SQLBIGVARCHAR:
case TdsEnums.SQLTEXT:
{
Debug.Assert(!isDataFeed || (value is TextDataFeed || value is XmlDataFeed), "Value must be a TextReader or XmlReader");
Debug.Assert(isDataFeed || (value is string || value is byte[]), "Value is a byte array or string");
if (isDataFeed)
{
Debug.Assert(type.IsPlp, "Stream assigned to non-PLP was not converted!");
TextDataFeed tdf = value as TextDataFeed;
if (tdf == null)
{
return NullIfCompletedWriteTask(WriteXmlFeed((XmlDataFeed)value, stateObj, needBom: true, encoding: _defaultEncoding, size: paramSize));
}
else
{
return NullIfCompletedWriteTask(WriteTextFeed(tdf, _defaultEncoding, false, stateObj, paramSize));
}
}
//.........这里部分代码省略.........
示例3: WriteBulkCopyMetaData
internal void WriteBulkCopyMetaData(_SqlMetaDataSet metadataCollection, int count, TdsParserStateObject stateObj)
{
if (!(State == TdsParserState.OpenNotLoggedIn || State == TdsParserState.OpenLoggedIn))
{
throw ADP.ClosedConnectionError();
}
stateObj.WriteByte(TdsEnums.SQLCOLMETADATA);
WriteShort(count, stateObj);
for (int i = 0; i < metadataCollection.Length; i++)
{
if (metadataCollection[i] != null)
{
_SqlMetaData md = metadataCollection[i];
// read user type - 4 bytes Yukon, 2 backwards
WriteInt(0x0, stateObj);
UInt16 flags;
flags = (UInt16)(md.updatability << 2);
flags |= (UInt16)(md.isNullable ? (UInt16)TdsEnums.Nullable : (UInt16)0);
flags |= (UInt16)(md.isIdentity ? (UInt16)TdsEnums.Identity : (UInt16)0);
WriteShort(flags, stateObj); // write the flags
switch (md.type)
{
case SqlDbType.Decimal:
stateObj.WriteByte(md.tdsType);
WriteTokenLength(md.tdsType, md.length, stateObj);
stateObj.WriteByte(md.precision);
stateObj.WriteByte(md.scale);
break;
case SqlDbType.Xml:
stateObj.WriteByteArray(s_xmlMetadataSubstituteSequence, s_xmlMetadataSubstituteSequence.Length, 0);
break;
case SqlDbType.Udt:
throw ADP.DbTypeNotSupported(SqlDbType.Udt.ToString());
case SqlDbType.Date:
stateObj.WriteByte(md.tdsType);
break;
case SqlDbType.Time:
case SqlDbType.DateTime2:
case SqlDbType.DateTimeOffset:
stateObj.WriteByte(md.tdsType);
stateObj.WriteByte(md.scale);
break;
default:
stateObj.WriteByte(md.tdsType);
WriteTokenLength(md.tdsType, md.length, stateObj);
if (md.metaType.IsCharType)
{
WriteUnsignedInt(md.collation.info, stateObj);
stateObj.WriteByte(md.collation.sortId);
}
break;
}
if (md.metaType.IsLong && !md.metaType.IsPlp)
{
WriteShort(md.tableName.Length, stateObj);
WriteString(md.tableName, stateObj);
}
stateObj.WriteByte((byte)md.column.Length);
WriteString(md.column, stateObj);
}
} // end for loop
}
示例4: WriteBulkCopyValue
//.........这里部分代码省略.........
case TdsEnums.SQLNCHAR:
case TdsEnums.SQLNVARCHAR:
case TdsEnums.SQLNTEXT:
ccb = ((isSqlType) ? ((SqlString)value).Value.Length : ((string)value).Length) * 2;
break;
case TdsEnums.SQLXMLTYPE:
// Value here could be string or XmlReader
if (value is XmlReader)
{
value = MetaType.GetStringFromXml((XmlReader)value);
}
ccb = ((isSqlType) ? ((SqlString)value).Value.Length : ((string)value).Length) * 2;
break;
default:
ccb = metadata.length;
break;
}
}
else
{
Debug.Assert(metatype.IsLong &&
((metatype.SqlDbType == SqlDbType.VarBinary && value is StreamDataFeed) ||
((metatype.SqlDbType == SqlDbType.VarChar || metatype.SqlDbType == SqlDbType.NVarChar) && value is TextDataFeed) ||
(metatype.SqlDbType == SqlDbType.Xml && value is XmlDataFeed)),
"Stream data feed should only be assigned to VarBinary(max), Text data feed should only be assigned to [N]VarChar(max), Xml data feed should only be assigned to XML(max)");
}
// Expected the text length in data stream for bulk copy of text, ntext, or image data.
//
if (metatype.IsLong)
{
switch (metatype.SqlDbType)
{
case SqlDbType.Text:
case SqlDbType.NText:
case SqlDbType.Image:
stateObj.WriteByteArray(s_longDataHeader, s_longDataHeader.Length, 0);
WriteTokenLength(metadata.tdsType, ccbStringBytes == 0 ? ccb : ccbStringBytes, stateObj);
break;
case SqlDbType.VarChar:
case SqlDbType.NVarChar:
case SqlDbType.VarBinary:
case SqlDbType.Xml:
case SqlDbType.Udt:
// plp data
WriteUnsignedLong(TdsEnums.SQL_PLP_UNKNOWNLEN, stateObj);
break;
}
}
else
{
WriteTokenLength(metadata.tdsType, ccbStringBytes == 0 ? ccb : ccbStringBytes, stateObj);
}
if (isSqlType)
{
internalWriteTask = WriteSqlValue(value, metatype, ccb, ccbStringBytes, 0, stateObj);
}
else if (metatype.SqlDbType != SqlDbType.Udt || metatype.IsLong)
{
internalWriteTask = WriteValue(value, metatype, metadata.scale, ccb, ccbStringBytes, 0, stateObj, metadata.length, isDataFeed);
if ((internalWriteTask == null) && (_asyncWrite))
{
internalWriteTask = stateObj.WaitForAccumulatedWrites();
}
Debug.Assert(_asyncWrite || stateObj.WaitForAccumulatedWrites() == null, "Should not have accumulated writes when writing sync");
}
else
{
WriteShort(ccb, stateObj);
internalWriteTask = stateObj.WriteByteArray((byte[])value, ccb, 0);
}
#if DEBUG
//In DEBUG mode, when SetAlwaysTaskOnWrite is true, we create a task. Allows us to verify async execution paths.
if (_asyncWrite && internalWriteTask == null && SqlBulkCopy.SetAlwaysTaskOnWrite == true)
{
internalWriteTask = Task.FromResult<object>(null);
}
#endif
if (internalWriteTask != null)
{ //i.e. the write was async.
resultTask = WriteBulkCopyValueSetupContinuation(internalWriteTask, saveEncoding, saveCollation, saveCodePage, saveLCID);
}
}
finally
{
if (internalWriteTask == null)
{
_defaultEncoding = saveEncoding;
_defaultCollation = saveCollation;
_defaultCodePage = saveCodePage;
_defaultLCID = saveLCID;
}
}
return resultTask;
}
示例5: WriteString
internal Task WriteString(string s, int length, int offset, TdsParserStateObject stateObj, bool canAccumulate = true)
{
int cBytes = ADP.CharSize * length;
// Perf shortcut: If it fits, write directly to the outBuff
if (cBytes < (stateObj._outBuff.Length - stateObj._outBytesUsed))
{
CopyStringToBytes(s, offset, stateObj._outBuff, stateObj._outBytesUsed, length);
stateObj._outBytesUsed += cBytes;
return null;
}
else
{
if (stateObj._bTmp == null || stateObj._bTmp.Length < cBytes)
{
stateObj._bTmp = new byte[cBytes];
}
CopyStringToBytes(s, offset, stateObj._bTmp, 0, length);
return stateObj.WriteByteArray(stateObj._bTmp, cBytes, 0, canAccumulate);
}
}
示例6: WriteEncodingChar
private Task WriteEncodingChar(string s, int numChars, int offset, Encoding encoding, TdsParserStateObject stateObj, bool canAccumulate = true)
{
char[] charData;
byte[] byteData;
// if hitting 7.0 server, encoding will be null in metadata for columns or return values since
// 7.0 has no support for multiple code pages in data - single code page support only
if (encoding == null)
encoding = _defaultEncoding;
charData = s.ToCharArray(offset, numChars);
// Optimization: if the entire string fits in the current buffer, then copy it directly
int bytesLeft = stateObj._outBuff.Length - stateObj._outBytesUsed;
if ((numChars <= bytesLeft) && (encoding.GetMaxByteCount(charData.Length) <= bytesLeft))
{
int bytesWritten = encoding.GetBytes(charData, 0, charData.Length, stateObj._outBuff, stateObj._outBytesUsed);
stateObj._outBytesUsed += bytesWritten;
return null;
}
else
{
byteData = encoding.GetBytes(charData, 0, numChars);
Debug.Assert(byteData != null, "no data from encoding");
return stateObj.WriteByteArray(byteData, byteData.Length, 0, canAccumulate);
}
}
示例7: WriteSqlVariantValue
//
// Translates a com+ object -> SqlVariant
// when the type is ambiguous, we always convert to the bigger type
// note that we also write out the maxlen and actuallen members (4 bytes each)
// in addition to the SQLVariant structure
//
internal Task WriteSqlVariantValue(object value, int length, int offset, TdsParserStateObject stateObj, bool canAccumulate = true)
{
// handle null values
if (ADP.IsNull(value))
{
WriteInt(TdsEnums.FIXEDNULL, stateObj); //maxlen
WriteInt(TdsEnums.FIXEDNULL, stateObj); //actuallen
return null;
}
MetaType mt = MetaType.GetMetaTypeFromValue(value);
// Special case data type correction for SqlMoney inside a SqlVariant.
if ((TdsEnums.SQLNUMERICN == mt.TDSType) && (8 == length))
{
// The caller will coerce all SqlTypes to native CLR types, which means SqlMoney will
// coerce to decimal/SQLNUMERICN (via SqlMoney.Value call). In the case where the original
// value was SqlMoney the caller will also pass in the metadata length for the SqlMoney type
// which is 8 bytes. To honor the intent of the caller here we coerce this special case
// input back to SqlMoney from decimal/SQLNUMERICN.
mt = MetaType.GetMetaTypeFromValue(new SqlMoney((decimal)value));
}
if (mt.IsAnsiType)
{
length = GetEncodingCharLength((string)value, length, 0, _defaultEncoding);
}
// max and actual len are equal to
// SQLVARIANTSIZE {type (1 byte) + cbPropBytes (1 byte)} + cbPropBytes + length (actual length of data in bytes)
WriteInt(TdsEnums.SQLVARIANT_SIZE + mt.PropBytes + length, stateObj); // maxLen
WriteInt(TdsEnums.SQLVARIANT_SIZE + mt.PropBytes + length, stateObj); // actualLen
// write the SQLVariant header (type and cbPropBytes)
stateObj.WriteByte(mt.TDSType);
stateObj.WriteByte(mt.PropBytes);
// now write the actual PropBytes and data
switch (mt.TDSType)
{
case TdsEnums.SQLFLT4:
WriteFloat((Single)value, stateObj);
break;
case TdsEnums.SQLFLT8:
WriteDouble((Double)value, stateObj);
break;
case TdsEnums.SQLINT8:
WriteLong((Int64)value, stateObj);
break;
case TdsEnums.SQLINT4:
WriteInt((Int32)value, stateObj);
break;
case TdsEnums.SQLINT2:
WriteShort((Int16)value, stateObj);
break;
case TdsEnums.SQLINT1:
stateObj.WriteByte((byte)value);
break;
case TdsEnums.SQLBIT:
if ((bool)value == true)
stateObj.WriteByte(1);
else
stateObj.WriteByte(0);
break;
case TdsEnums.SQLBIGVARBINARY:
{
byte[] b = (byte[])value;
WriteShort(length, stateObj); // propbytes: varlen
return stateObj.WriteByteArray(b, length, offset, canAccumulate);
}
case TdsEnums.SQLBIGVARCHAR:
{
string s = (string)value;
WriteUnsignedInt(_defaultCollation.info, stateObj); // propbytes: collation.Info
stateObj.WriteByte(_defaultCollation.sortId); // propbytes: collation.SortId
WriteShort(length, stateObj); // propbyte: varlen
return WriteEncodingChar(s, _defaultEncoding, stateObj, canAccumulate);
}
case TdsEnums.SQLUNIQUEID:
{
System.Guid guid = (System.Guid)value;
byte[] b = guid.ToByteArray();
//.........这里部分代码省略.........
示例8: WriteSqlVariantDataRowValue
// todo: since we now know the difference between SqlWriteVariantValue and SqlWriteRowDataVariant we should consider
// combining these tow methods.
//
// Translates a com+ object -> SqlVariant
// when the type is ambiguous, we always convert to the bigger type
// note that we also write out the maxlen and actuallen members (4 bytes each)
// in addition to the SQLVariant structure
//
// Devnote: DataRows are preceeded by Metadata. The Metadata includes the MaxLen value.
// Therefore the sql_variant value must not include the MaxLength. This is the major difference
// between this method and WriteSqlVariantValue above.
//
internal Task WriteSqlVariantDataRowValue(object value, TdsParserStateObject stateObj, bool canAccumulate = true)
{
// handle null values
if ((null == value) || (DBNull.Value == value))
{
WriteInt(TdsEnums.FIXEDNULL, stateObj);
return null;
}
MetaType metatype = MetaType.GetMetaTypeFromValue(value);
int length = 0;
if (metatype.IsAnsiType)
{
length = GetEncodingCharLength((string)value, length, 0, _defaultEncoding);
}
switch (metatype.TDSType)
{
case TdsEnums.SQLFLT4:
WriteSqlVariantHeader(6, metatype.TDSType, metatype.PropBytes, stateObj);
WriteFloat((Single)value, stateObj);
break;
case TdsEnums.SQLFLT8:
WriteSqlVariantHeader(10, metatype.TDSType, metatype.PropBytes, stateObj);
WriteDouble((Double)value, stateObj);
break;
case TdsEnums.SQLINT8:
WriteSqlVariantHeader(10, metatype.TDSType, metatype.PropBytes, stateObj);
WriteLong((Int64)value, stateObj);
break;
case TdsEnums.SQLINT4:
WriteSqlVariantHeader(6, metatype.TDSType, metatype.PropBytes, stateObj);
WriteInt((Int32)value, stateObj);
break;
case TdsEnums.SQLINT2:
WriteSqlVariantHeader(4, metatype.TDSType, metatype.PropBytes, stateObj);
WriteShort((Int16)value, stateObj);
break;
case TdsEnums.SQLINT1:
WriteSqlVariantHeader(3, metatype.TDSType, metatype.PropBytes, stateObj);
stateObj.WriteByte((byte)value);
break;
case TdsEnums.SQLBIT:
WriteSqlVariantHeader(3, metatype.TDSType, metatype.PropBytes, stateObj);
if ((bool)value == true)
stateObj.WriteByte(1);
else
stateObj.WriteByte(0);
break;
case TdsEnums.SQLBIGVARBINARY:
{
byte[] b = (byte[])value;
length = b.Length;
WriteSqlVariantHeader(4 + length, metatype.TDSType, metatype.PropBytes, stateObj);
WriteShort(length, stateObj); // propbytes: varlen
return stateObj.WriteByteArray(b, length, 0, canAccumulate);
}
case TdsEnums.SQLBIGVARCHAR:
{
string s = (string)value;
length = s.Length;
WriteSqlVariantHeader(9 + length, metatype.TDSType, metatype.PropBytes, stateObj);
WriteUnsignedInt(_defaultCollation.info, stateObj); // propbytes: collation.Info
stateObj.WriteByte(_defaultCollation.sortId); // propbytes: collation.SortId
WriteShort(length, stateObj);
return WriteEncodingChar(s, _defaultEncoding, stateObj, canAccumulate);
}
case TdsEnums.SQLUNIQUEID:
{
System.Guid guid = (System.Guid)value;
byte[] b = guid.ToByteArray();
length = b.Length;
Debug.Assert(length == 16, "Invalid length for guid type in com+ object");
//.........这里部分代码省略.........
示例9: WriteTraceHeaderData
// Write the trace header data, not including the trace header length
private void WriteTraceHeaderData(TdsParserStateObject stateObj) {
Debug.Assert(this.IncludeTraceHeader, "WriteTraceHeaderData can only be called on a Denali or higher version server and bid trace with the control bit are on");
// We may need to update the trace header length if trace header is changed in the future
ActivityCorrelator.ActivityId actId = ActivityCorrelator.Current;
WriteShort(TdsEnums.HEADERTYPE_TRACE, stateObj); // Trace Header Type
stateObj.WriteByteArray(actId.Id.ToByteArray(), GUID_SIZE, 0); // Id (Guid)
WriteUnsignedInt(actId.Sequence, stateObj); // sequence number
Bid.Trace("<sc.TdsParser.WriteTraceHeaderData|INFO> ActivityID %ls\n", actId.ToString());
}
示例10: WriteDouble
//
// Takes a double and writes it as a 64 bit double.
//
internal void WriteDouble(double v, TdsParserStateObject stateObj)
{
byte[] bytes = BitConverter.GetBytes(v);
stateObj.WriteByteArray(bytes, bytes.Length, 0);
}
示例11: WriteBulkCopyMetaData
internal void WriteBulkCopyMetaData(_SqlMetaDataSet metadataCollection, int count, TdsParserStateObject stateObj) {
if (!(State == TdsParserState.OpenNotLoggedIn || State == TdsParserState.OpenLoggedIn)) {
throw ADP.ClosedConnectionError();
}
stateObj.WriteByte(TdsEnums.SQLCOLMETADATA);
WriteShort(count, stateObj);
// Write CEK table - 0 count
WriteCekTable(metadataCollection, stateObj);
for (int i = 0; i < metadataCollection.Length; i++) {
if (metadataCollection[i] != null) {
_SqlMetaData md = metadataCollection[i];
// read user type - 4 bytes Yukon, 2 backwards
if (IsYukonOrNewer) {
WriteInt(0x0, stateObj);
}
else {
WriteShort(0x0000, stateObj);
}
// Write the flags
UInt16 flags;
flags = (UInt16)(md.updatability << 2);
flags |= (UInt16)(md.isNullable ? (UInt16)TdsEnums.Nullable : (UInt16)0);
flags |= (UInt16)(md.isIdentity ? (UInt16)TdsEnums.Identity : (UInt16)0);
// Write the next byte of flags
if (_serverSupportsColumnEncryption) { // TCE Supported
if (ShouldEncryptValuesForBulkCopy()) { // TCE enabled on connection options
flags |= (UInt16)(md.isEncrypted ? (UInt16)(TdsEnums.IsEncrypted << 8) : (UInt16)0);
}
}
WriteShort(flags, stateObj);// write the flags
// todo:
// for xml WriteTokenLength results in a no-op
// discuss this with blaine ...
// ([....]) xml datatype does not have token length in its metadata. So it should be a noop.
switch (md.type) {
case SqlDbType.Decimal:
stateObj.WriteByte(md.tdsType);
WriteTokenLength(md.tdsType, md.length, stateObj);
stateObj.WriteByte(md.precision);
stateObj.WriteByte(md.scale);
break;
case SqlDbType.Xml:
//
stateObj.WriteByteArray(s_xmlMetadataSubstituteSequence, s_xmlMetadataSubstituteSequence.Length, 0);
break;
case SqlDbType.Udt:
stateObj.WriteByte(TdsEnums.SQLBIGVARBINARY);
WriteTokenLength(TdsEnums.SQLBIGVARBINARY, md.length, stateObj);
break;
case SqlDbType.Date:
stateObj.WriteByte(md.tdsType);
break;
case SqlDbType.Time:
case SqlDbType.DateTime2:
case SqlDbType.DateTimeOffset:
stateObj.WriteByte(md.tdsType);
stateObj.WriteByte(md.scale);
break;
default:
stateObj.WriteByte(md.tdsType);
WriteTokenLength(md.tdsType, md.length, stateObj);
if (md.metaType.IsCharType && _isShiloh) {
WriteUnsignedInt(md.collation.info, stateObj);
stateObj.WriteByte(md.collation.sortId);
}
break;
}
if (md.metaType.IsLong && !md.metaType.IsPlp) {
WriteShort(md.tableName.Length, stateObj);
WriteString(md.tableName, stateObj);
}
WriteCryptoMetadata(md, stateObj);
stateObj.WriteByte((byte)md.column.Length);
WriteString(md.column, stateObj);
}
} // end for loop
}
示例12: WriteEncryptionEntries
/// <summary>
/// Writes a single entry of CEK Table into TDS Stream (for bulk copy).
/// </summary>
/// <returns></returns>
internal void WriteEncryptionEntries (ref SqlTceCipherInfoTable cekTable, TdsParserStateObject stateObj) {
for (int i =0; i < cekTable.Size; i++) {
// Write Db ID
WriteInt(cekTable[i].DatabaseId, stateObj);
// Write Key ID
WriteInt(cekTable[i].CekId, stateObj);
// Write Key Version
WriteInt(cekTable[i].CekVersion, stateObj);
// Write 8 bytes of key MD Version
Debug.Assert (8 == cekTable[i].CekMdVersion.Length);
stateObj.WriteByteArray (cekTable[i].CekMdVersion, 8, 0);
// We don't really need to send the keys
stateObj.WriteByte(0x00);
}
}
示例13: TdsExecuteRPC
//.........这里部分代码省略.........
size = udtVal.Length;
//it may be legitimate, but we dont support it yet
if (size < 0 || (size >= UInt16.MaxValue && maxsize != -1))
throw new IndexOutOfRangeException();
}
//if this is NULL value, write special null value
byte[] lenBytes = BitConverter.GetBytes((Int64)size);
if (ADP.IsEmpty(param.UdtTypeName))
throw SQL.MustSetUdtTypeNameForUdtParams();
// Split the input name. TypeName is returned as single 3 part name during DeriveParameters.
// NOTE: ParseUdtTypeName throws if format is incorrect
String[] names = SqlParameter.ParseTypeName(param.UdtTypeName, true /* is UdtTypeName */);
if (!ADP.IsEmpty(names[0]) && TdsEnums.MAX_SERVERNAME < names[0].Length) {
throw ADP.ArgumentOutOfRange("names");
}
if (!ADP.IsEmpty(names[1]) && TdsEnums.MAX_SERVERNAME < names[names.Length - 2].Length) {
throw ADP.ArgumentOutOfRange("names");
}
if (TdsEnums.MAX_SERVERNAME < names[2].Length) {
throw ADP.ArgumentOutOfRange("names");
}
WriteUDTMetaData(value, names[0], names[1], names[2], stateObj);
//
if (!isNull) {
WriteUnsignedLong((ulong)udtVal.Length, stateObj); // PLP length
if (udtVal.Length > 0) { // Only write chunk length if its value is greater than 0
WriteInt(udtVal.Length, stateObj); // Chunk length
stateObj.WriteByteArray(udtVal, udtVal.Length, 0); // Value
}
WriteInt(0, stateObj); // Terminator
}
else {
WriteUnsignedLong(TdsEnums.SQL_PLP_NULL, stateObj); // PLP Null.
}
continue; // End of UDT - continue to next parameter.
//
}
else if (mt.IsPlp) {
if (mt.SqlDbType != SqlDbType.Xml)
WriteShort(TdsEnums.SQL_USHORTVARMAXLEN, stateObj);
}
else if ((!mt.IsVarTime) && (mt.SqlDbType != SqlDbType.Date)) { // Time, Date, DateTime2, DateTimeoffset do not have the size written out
maxsize = (size > actualSize) ? size : actualSize;
if (maxsize == 0 && IsYukonOrNewer) {
// Yukon doesn't like 0 as MaxSize. Change it to 2 for unicode types (SQL9 - 682322)
if (mt.IsNCharType)
maxsize = 2;
else
maxsize = 1;
}
WriteParameterVarLen(mt, maxsize, false/*IsNull*/, stateObj);
}
}
// scale and precision are only relevant for numeric and decimal types
if (mt.SqlDbType == SqlDbType.Decimal) {
if (0 == precision) {
if (_isShiloh)
stateObj.WriteByte(TdsEnums.DEFAULT_NUMERIC_PRECISION);
示例14: TdsExecuteTransactionManagerRequest
internal SqlDataReader TdsExecuteTransactionManagerRequest(
byte[] buffer,
TdsEnums.TransactionManagerRequestType request,
string transactionName,
TdsEnums.TransactionManagerIsolationLevel isoLevel,
int timeout,
SqlInternalTransaction transaction,
TdsParserStateObject stateObj,
bool isDelegateControlRequest) {
Debug.Assert(this == stateObj.Parser, "different parsers");
if (TdsParserState.Broken == State || TdsParserState.Closed == State) {
return null;
}
// SQLBUDT #20010853 - Promote, Commit and Rollback requests for
// delegated transactions often happen while there is an open result
// set, so we need to handle them by using a different MARS session,
// otherwise we'll write on the physical state objects while someone
// else is using it. When we don't have MARS enabled, we need to
// lock the physical state object to syncronize it's use at least
// until we increment the open results count. Once it's been
// incremented the delegated transaction requests will fail, so they
// won't stomp on anything.
Debug.Assert(!_connHandler.ThreadHasParserLockForClose || _connHandler._parserLock.ThreadMayHaveLock(), "Thread claims to have parser lock, but lock is not taken");
bool callerHasConnectionLock = _connHandler.ThreadHasParserLockForClose; // If the thread already claims to have the parser lock, then we will let the caller handle releasing it
if (!callerHasConnectionLock) {
_connHandler._parserLock.Wait(canReleaseFromAnyThread:false);
_connHandler.ThreadHasParserLockForClose = true;
}
// Capture _asyncWrite (after taking lock) to restore it afterwards
bool hadAsyncWrites = _asyncWrite;
try {
// Temprarily disable async writes
_asyncWrite = false;
// This validation step MUST be done after locking the connection to guarantee we don't
// accidentally execute after the transaction has completed on a different thread.
if (!isDelegateControlRequest) {
_connHandler.CheckEnlistedTransactionBinding();
}
stateObj._outputMessageType = TdsEnums.MT_TRANS; // set message type
stateObj.SetTimeoutSeconds(timeout);
stateObj.SniContext = SniContext.Snix_Execute;
if (_isYukon) {
const int marsHeaderSize = 18; // 4 + 2 + 8 + 4
const int totalHeaderLength = 22; // 4 + 4 + 2 + 8 + 4
Debug.Assert(stateObj._outBytesUsed == stateObj._outputHeaderLen, "Output bytes written before total header length");
// Write total header length
WriteInt(totalHeaderLength, stateObj);
// Write mars header length
WriteInt(marsHeaderSize, stateObj);
WriteMarsHeaderData(stateObj, _currentTransaction);
}
WriteShort((short)request, stateObj); // write TransactionManager Request type
bool returnReader = false;
switch (request) {
case TdsEnums.TransactionManagerRequestType.GetDTCAddress:
WriteShort(0, stateObj);
returnReader = true;
break;
case TdsEnums.TransactionManagerRequestType.Propagate:
if (null != buffer) {
WriteShort(buffer.Length, stateObj);
stateObj.WriteByteArray(buffer, buffer.Length, 0);
}
else {
WriteShort(0, stateObj);
}
break;
case TdsEnums.TransactionManagerRequestType.Begin:
Debug.Assert(IsYukonOrNewer, "Should not be calling TdsExecuteTransactionManagerRequest on pre-Yukon clients for BeginTransaction!");
Debug.Assert(null != transaction, "Should have specified an internalTransaction when doing a BeginTransaction request!");
// Only assign the passed in transaction if it is not equal to the current transaction.
// And, if it is not equal, the current actually should be null. Anything else
// is a unexpected state. The concern here is mainly for the mixed use of
// T-SQL and API transactions. See SQL BU DT 345300 for full details and repro.
// Expected states:
// 1) _pendingTransaction = null, _currentTransaction = null, non null transaction
// passed in on BeginTransaction API call.
// 2) _currentTransaction != null, _pendingTransaction = null, non null transaction
// passed in but equivalent to _currentTransaction.
// #1 will occur on standard BeginTransactionAPI call. #2 should only occur if
// t-sql transaction started followed by a call to SqlConnection.BeginTransaction.
// Any other state is unknown.
if (_currentTransaction != transaction) {
Debug.Assert(_currentTransaction == null || true == _fResetConnection, "We should not have a current Tx at this point");
//.........这里部分代码省略.........
示例15: WriteEncryptionMetadata
/// <summary>
/// Write parameter encryption metadata.
/// </summary>
private void WriteEncryptionMetadata(SqlColumnEncryptionInputParameterInfo columnEncryptionParameterInfo, TdsParserStateObject stateObj) {
Debug.Assert(columnEncryptionParameterInfo != null, @"columnEncryptionParameterInfo cannot be null");
Debug.Assert(stateObj != null, @"stateObj cannot be null");
// Write the TypeInfo.
WriteSmiTypeInfo(columnEncryptionParameterInfo.ParameterMetadata, stateObj);
// Write the serialized array in columnEncryptionParameterInfo.
stateObj.WriteByteArray(columnEncryptionParameterInfo.SerializedWireFormat,
columnEncryptionParameterInfo.SerializedWireFormat.Length,
offsetBuffer: 0);
}