Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,6 @@ internal EncryptionOptions EncryptionOptions
}
}

internal bool Is2005OrNewer => true;

internal bool Is2008OrNewer
{
get
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,6 @@ internal object[] CreateRowBuffer()
/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlBulkCopy.xml' path='docs/members[@name="SqlBulkCopy"]/SqlBulkCopy/*'/>
public sealed class SqlBulkCopy : IDisposable
{
private enum TableNameComponents
{
Server = 0,
Catalog,
Owner,
TableName,
}

private enum ValueSourceType
{
Unspecified = 0,
Expand Down Expand Up @@ -161,10 +153,6 @@ public SourceColumnMetadata(ValueMethod method, bool isSqlType, bool isDataFeed)
// MetaData has n columns but no rows
// Collation has 4 columns and n rows

private const int TranCountResultId = 0;
private const int TranCountRowId = 0;
private const int TranCountValueId = 0;

private const int MetaDataResultId = 1;

private const int CollationResultId = 2;
Expand Down Expand Up @@ -439,66 +427,60 @@ private string CreateInitialQuery()
string TDSCommand;

TDSCommand = "select @@trancount; SET FMTONLY ON select * from " + ADP.BuildMultiPartName(parts) + " SET FMTONLY OFF ";
if (_connection.Is2000)
{
// If its a temp DB then try to connect

string TableCollationsStoredProc;
if (_connection.Is2008OrNewer)
{
TableCollationsStoredProc = "sp_tablecollations_100";
}
else if (_connection.Is2005OrNewer)
{
TableCollationsStoredProc = "sp_tablecollations_90";
}
else
{
TableCollationsStoredProc = "sp_tablecollations";
}
// If its a temp DB then try to connect

string TableName = parts[MultipartIdentifier.TableIndex];
bool isTempTable = TableName.Length > 0 && '#' == TableName[0];
if (!ADP.IsEmpty(TableName))
{
// Escape table name to be put inside TSQL literal block (within N'').
TableName = SqlServerEscapeHelper.EscapeStringAsLiteral(TableName);
// VSDD 581951 - escape the table name
TableName = SqlServerEscapeHelper.EscapeIdentifier(TableName);
}
string TableCollationsStoredProc;
if (_connection.Is2008OrNewer)
{
TableCollationsStoredProc = "sp_tablecollations_100";
}
else
{
TableCollationsStoredProc = "sp_tablecollations_90";
}

string SchemaName = parts[MultipartIdentifier.SchemaIndex];
if (!ADP.IsEmpty(SchemaName))
{
// Escape schema name to be put inside TSQL literal block (within N'').
SchemaName = SqlServerEscapeHelper.EscapeStringAsLiteral(SchemaName);
// VSDD 581951 - escape the schema name
SchemaName = SqlServerEscapeHelper.EscapeIdentifier(SchemaName);
}
string TableName = parts[MultipartIdentifier.TableIndex];
bool isTempTable = TableName.Length > 0 && '#' == TableName[0];
if (!ADP.IsEmpty(TableName))
{
// Escape table name to be put inside TSQL literal block (within N'').
TableName = SqlServerEscapeHelper.EscapeStringAsLiteral(TableName);
// VSDD 581951 - escape the table name
TableName = SqlServerEscapeHelper.EscapeIdentifier(TableName);
}

string CatalogName = parts[MultipartIdentifier.CatalogIndex];
if (isTempTable && ADP.IsEmpty(CatalogName))
{
TDSCommand += string.Format("exec tempdb..{0} N'{1}.{2}'",
TableCollationsStoredProc,
SchemaName,
TableName
);
}
else
string SchemaName = parts[MultipartIdentifier.SchemaIndex];
if (!ADP.IsEmpty(SchemaName))
{
// Escape schema name to be put inside TSQL literal block (within N'').
SchemaName = SqlServerEscapeHelper.EscapeStringAsLiteral(SchemaName);
// VSDD 581951 - escape the schema name
SchemaName = SqlServerEscapeHelper.EscapeIdentifier(SchemaName);
}

string CatalogName = parts[MultipartIdentifier.CatalogIndex];
if (isTempTable && ADP.IsEmpty(CatalogName))
{
TDSCommand += string.Format("exec tempdb..{0} N'{1}.{2}'",
TableCollationsStoredProc,
SchemaName,
TableName
);
}
else
{
// VSDD 581951 - escape the catalog name
if (!ADP.IsEmpty(CatalogName))
{
// VSDD 581951 - escape the catalog name
if (!ADP.IsEmpty(CatalogName))
{
CatalogName = SqlServerEscapeHelper.EscapeIdentifier(CatalogName);
}
TDSCommand += string.Format("exec {0}..{1} N'{2}.{3}'",
CatalogName,
TableCollationsStoredProc,
SchemaName,
TableName
);
CatalogName = SqlServerEscapeHelper.EscapeIdentifier(CatalogName);
}
TDSCommand += string.Format("exec {0}..{1} N'{2}.{3}'",
CatalogName,
TableCollationsStoredProc,
SchemaName,
TableName
);
}
return TDSCommand;
}
Expand Down Expand Up @@ -549,7 +531,7 @@ private string AnalyzeTargetAndCreateUpdateBulkCommand(BulkCopySimpleResultSet i

StringBuilder updateBulkCommandText = new StringBuilder();

if (_connection.Is2000 && 0 == internalResults[CollationResultId].Count)
if (0 == internalResults[CollationResultId].Count)
{
throw SQL.BulkLoadNoCollation();
}
Expand All @@ -562,15 +544,7 @@ private string AnalyzeTargetAndCreateUpdateBulkCommand(BulkCopySimpleResultSet i

bool isInTransaction;

if (_parser.Is2005OrNewer)
{
isInTransaction = _connection.HasLocalTransaction;
}
else
{
isInTransaction = (bool)(0 < (SqlInt32)(internalResults[TranCountResultId][TranCountRowId][TranCountValueId]));
}

isInTransaction = _connection.HasLocalTransaction;
// Throw if there is a transaction but no flag is set
if (isInTransaction &&
_externalTransaction == null &&
Expand Down Expand Up @@ -701,50 +675,45 @@ private string AnalyzeTargetAndCreateUpdateBulkCommand(BulkCopySimpleResultSet i
}
}

if (_connection.Is2000)
{
// 2000 or above!
// get collation for column i
// Get collation for column i
Result rowset = internalResults[CollationResultId];
object rowvalue = rowset[i][CollationId];

Result rowset = internalResults[CollationResultId];
object rowvalue = rowset[i][CollationId];
bool shouldSendCollation;
switch (metadata.type)
{
case SqlDbType.Char:
case SqlDbType.NChar:
case SqlDbType.VarChar:
case SqlDbType.NVarChar:
case SqlDbType.Text:
case SqlDbType.NText:
shouldSendCollation = true;
break;

bool shouldSendCollation;
switch (metadata.type)
{
case SqlDbType.Char:
case SqlDbType.NChar:
case SqlDbType.VarChar:
case SqlDbType.NVarChar:
case SqlDbType.Text:
case SqlDbType.NText:
shouldSendCollation = true;
break;
default:
shouldSendCollation = false;
break;
}

default:
shouldSendCollation = false;
break;
}
if (rowvalue != null && shouldSendCollation)
{
Debug.Assert(rowvalue is SqlString);
SqlString collation_name = (SqlString)rowvalue;

if (rowvalue != null && shouldSendCollation)
if (!collation_name.IsNull)
{
Debug.Assert(rowvalue is SqlString);
SqlString collation_name = (SqlString)rowvalue;

if (!collation_name.IsNull)
updateBulkCommandText.Append(" COLLATE " + collation_name.Value);
// VSTFDEVDIV 461426: compare collations only if the collation value was set on the metadata
if (_sqlDataReaderRowSource != null && metadata.collation != null)
{
updateBulkCommandText.Append(" COLLATE " + collation_name.Value);
// VSTFDEVDIV 461426: compare collations only if the collation value was set on the metadata
if (_sqlDataReaderRowSource != null && metadata.collation != null)
// On SqlDataReader we can verify the sourcecolumn collation!
int sourceColumnId = _localColumnMappings[assocId]._internalSourceColumnOrdinal;
int destinationLcid = metadata.collation.LCID;
int sourceLcid = _sqlDataReaderRowSource.GetLocaleId(sourceColumnId);
if (sourceLcid != destinationLcid)
{
// On SqlDataReader we can verify the sourcecolumn collation!
int sourceColumnId = _localColumnMappings[assocId]._internalSourceColumnOrdinal;
int destinationLcid = metadata.collation.LCID;
int sourceLcid = _sqlDataReaderRowSource.GetLocaleId(sourceColumnId);
if (sourceLcid != destinationLcid)
{
throw SQL.BulkLoadLcidMismatch(sourceLcid, _sqlDataReaderRowSource.GetName(sourceColumnId), destinationLcid, metadata.column);
}
throw SQL.BulkLoadLcidMismatch(sourceLcid, _sqlDataReaderRowSource.GetName(sourceColumnId), destinationLcid, metadata.column);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -598,17 +598,6 @@ private SqlInternalConnectionTds InternalTdsConnection
}
}

private bool Is2000
{
get
{
Debug.Assert(_activeConnection != null, "The active connection is null!");
if (_activeConnection == null)
return false;
return _activeConnection.Is2000;
}
}

private bool IsProviderRetriable => SqlConfigurableRetryFactory.IsRetriable(RetryLogicProvider);

/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlCommand.xml' path='docs/members[@name="SqlCommand"]/RetryLogicProvider/*' />
Expand Down Expand Up @@ -3441,16 +3430,8 @@ internal void DeriveParameters()
}
else
{
if (this.Connection.Is2005OrNewer)
{
// Procedure - [sp_procedure_params_managed]
cmdText.Append("[sys].[").Append(TdsEnums.SP_PARAMS_MANAGED).Append("]");
}
else
{
// Procedure - [sp_procedure_params_rowset]
cmdText.Append(".[").Append(TdsEnums.SP_PARAMS).Append("]");
}
// Procedure - [sp_procedure_params_managed]
cmdText.Append("[sys].[").Append(TdsEnums.SP_PARAMS_MANAGED).Append("]");

colNames = PreSql2008ProcParamsNames;
useManagedDataType = false;
Expand Down Expand Up @@ -3567,9 +3548,6 @@ internal void DeriveParameters()
// type name for Udt
if (SqlDbType.Udt == p.SqlDbType)
{

Debug.Assert(this._activeConnection.Is2005OrNewer, "Invalid datatype token received from pre-2005 server");

string udtTypeName;
if (useManagedDataType)
{
Expand Down Expand Up @@ -3693,19 +3671,16 @@ private void CheckNotificationStateAndAutoEnlist()
// present. If so, auto enlist to the dependency ID given in the context data.
if (NotificationAutoEnlist)
{
if (_activeConnection.Is2005OrNewer)
{ // Only supported for 2005...
string notifyContext = SqlNotificationContext();
if (!ADP.IsEmpty(notifyContext))
{
// Map to dependency by ID set in context data.
SqlDependency dependency = SqlDependencyPerAppDomainDispatcher.SingletonInstance.LookupDependencyEntry(notifyContext);
string notifyContext = SqlNotificationContext();
if (!ADP.IsEmpty(notifyContext))
{
// Map to dependency by ID set in context data.
SqlDependency dependency = SqlDependencyPerAppDomainDispatcher.SingletonInstance.LookupDependencyEntry(notifyContext);

if (dependency != null)
{
// Add this command to the dependency.
dependency.AddCommandDependency(this);
}
if (dependency != null)
{
// Add this command to the dependency.
dependency.AddCommandDependency(this);
}
}
}
Expand Down Expand Up @@ -5342,7 +5317,6 @@ private SqlDataReader RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavi
}
else if (_execType == EXECTYPE.PREPAREPENDING)
{
Debug.Assert(_activeConnection.Is2000, "Invalid attempt to call sp_prepexec on non 7.x server");
rpc = BuildPrepExec(cmdBehavior);
// next time through, only do an exec
_execType = EXECTYPE.PREPARED;
Expand All @@ -5358,8 +5332,7 @@ private SqlDataReader RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavi
}

// if 2000, then set NOMETADATA_UNLESSCHANGED flag
if (_activeConnection.Is2000)
rpc.options = TdsEnums.RPC_NOMETADATA;
rpc.options = TdsEnums.RPC_NOMETADATA;
if (returnStream)
{
SqlClientEventSource.Log.TryTraceEvent("<sc.SqlCommand.ExecuteReader|INFO> {0}, Command executed as RPC.", ObjectID);
Expand All @@ -5372,10 +5345,6 @@ private SqlDataReader RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavi
else
{
Debug.Assert(this.CommandType == System.Data.CommandType.StoredProcedure, "unknown command type!");
// note: invalid asserts on 2000. On 8.0 (2000) and above a command is ALWAYS prepared
// and IsDirty is always set if there are changes and the command is marked Prepared!
Debug.Assert(Is2000 || !IsPrepared, "RPC should not be prepared!");
Debug.Assert(Is2000 || !IsDirty, "RPC should not be marked as dirty!");

BuildRPC(inSchema, _parameters, ref rpc);

Expand Down Expand Up @@ -5813,12 +5782,6 @@ private void ValidateCommand(string method, bool async)
throw ADP.CommandTextRequired(method);
}

// Notification property must be null for pre-2005 connections
if ((Notification != null) && !_activeConnection.Is2005OrNewer)
{
throw SQL.NotificationsRequire2005();
}

if ((async) && (_activeConnection.IsContextConnection))
{
// Async not supported on Context Connections
Expand Down Expand Up @@ -6849,14 +6812,7 @@ internal string BuildParamList(TdsParser parser, SqlParameterCollection paramete

if (0 == precision)
{
if (Is2000)
{
precision = TdsEnums.DEFAULT_NUMERIC_PRECISION;
}
else
{
precision = TdsEnums.SQL70_DEFAULT_NUMERIC_PRECISION;
}
precision = TdsEnums.DEFAULT_NUMERIC_PRECISION;
}

paramList.Append(precision);
Expand Down
Loading