Skip to content

Commit 523a1ee

Browse files
Bacporting Fix | Default UTF8 collation conflict (#1910)
[3.1.2] Backport: Fix | Default UTF8 collation conflict #1910
1 parent 7f078be commit 523a1ee

File tree

2 files changed

+62
-9
lines changed

2 files changed

+62
-9
lines changed

src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParser.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3063,20 +3063,13 @@ private bool TryProcessEnvChange(int tokenLength, TdsParserStateObject stateObj,
30633063
_defaultCollation = env.newCollation;
30643064
_defaultLCID = env.newCollation.LCID;
30653065

3066-
int newCodePage = GetCodePage(env.newCollation, stateObj);
3067-
30683066
if ((env.newCollation.info & TdsEnums.UTF8_IN_TDSCOLLATION) == TdsEnums.UTF8_IN_TDSCOLLATION)
30693067
{ // UTF8 collation
30703068
_defaultEncoding = Encoding.UTF8;
3071-
3072-
if (newCodePage != _defaultCodePage)
3073-
{
3074-
_defaultCodePage = newCodePage;
3075-
}
30763069
}
30773070
else
30783071
{
3079-
3072+
int newCodePage = GetCodePage(env.newCollation, stateObj);
30803073
if (newCodePage != _defaultCodePage)
30813074
{
30823075
_defaultCodePage = newCodePage;

src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/Utf8SupportTest/Utf8SupportTest.cs

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using System.Text;
6+
using System;
57
using Xunit;
68

79
namespace Microsoft.Data.SqlClient.ManualTesting.Tests
@@ -29,6 +31,64 @@ public static void CheckSupportUtf8ConnectionProperty()
2931
}
3032
}
3133

32-
// TODO: Write tests using UTF8 collations
34+
// skip creating database on Azure
35+
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup), nameof(DataTestUtility.IsNotAzureServer), nameof(DataTestUtility.IsNotAzureSynapse))]
36+
public static void UTF8databaseTest()
37+
{
38+
const string letters = @"!\#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\u007f€\u0081‚ƒ„…†‡ˆ‰Š‹Œ\u008dŽ\u008f\u0090‘’“”•–—˜™š›œ\u009džŸ ¡¢£¤¥¦§¨©ª«¬­®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖרÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ";
39+
string dbName = DataTestUtility.GetUniqueNameForSqlServer("UTF8databaseTest", false);
40+
string tblName = "Table1";
41+
42+
SqlConnectionStringBuilder builder = new(DataTestUtility.TCPConnectionString);
43+
builder.InitialCatalog = "master";
44+
45+
using SqlConnection cn = new(builder.ConnectionString);
46+
cn.Open();
47+
48+
try
49+
{
50+
PrepareDatabaseUTF8(cn, dbName, tblName, letters);
51+
52+
builder.InitialCatalog = dbName;
53+
using SqlConnection cnnTest = new(builder.ConnectionString);
54+
// creating a databse is a time consumer action and could be retried.
55+
SqlRetryLogicOption retryOption = new() { NumberOfTries = 3, DeltaTime = TimeSpan.FromMilliseconds(200) };
56+
cnnTest.RetryLogicProvider = SqlConfigurableRetryFactory.CreateIncrementalRetryProvider(retryOption);
57+
cnnTest.Open();
58+
59+
using SqlCommand cmd = cnnTest.CreateCommand();
60+
cmd.CommandText = $"SELECT * FROM {tblName}";
61+
62+
using SqlDataReader reader = cmd.ExecuteReader();
63+
64+
Assert.True(reader.Read(), "The test table should have a row!");
65+
object[] data = new object[1];
66+
reader.GetSqlValues(data);
67+
Assert.Equal(letters, data[0].ToString());
68+
reader.Close();
69+
cnnTest.Close();
70+
}
71+
finally
72+
{
73+
DataTestUtility.DropDatabase(cn, dbName);
74+
}
75+
}
76+
77+
private static void PrepareDatabaseUTF8(SqlConnection cnn, string dbName, string tblName, string letters)
78+
{
79+
StringBuilder sb = new();
80+
81+
using SqlCommand cmd = cnn.CreateCommand();
82+
83+
cmd.CommandText = $"CREATE DATABASE [{dbName}] COLLATE Latin1_General_100_CI_AS_SC_UTF8;";
84+
cmd.ExecuteNonQuery();
85+
86+
sb.AppendLine($"CREATE TABLE [{dbName}].dbo.[{tblName}] (col VARCHAR(7633) COLLATE Latin1_General_100_CI_AS_SC);");
87+
sb.AppendLine($"INSERT INTO [{dbName}].dbo.[{tblName}] VALUES (@letters);");
88+
89+
cmd.Parameters.Add(new SqlParameter("letters", letters));
90+
cmd.CommandText = sb.ToString();
91+
cmd.ExecuteNonQuery();
92+
}
3393
}
3494
}

0 commit comments

Comments
 (0)