Skip to content

Commit 200e442

Browse files
[Hotfix 4.1.1] | Fix default UTF8 collation conflict (#1739) (#1749)
1 parent 8b4022b commit 200e442

File tree

2 files changed

+63
-8
lines changed

2 files changed

+63
-8
lines changed

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3080,20 +3080,14 @@ private bool TryProcessEnvChange(int tokenLength, TdsParserStateObject stateObj,
30803080
_defaultCollation = env.newCollation;
30813081
_defaultLCID = env.newCollation.LCID;
30823082

3083-
int newCodePage = GetCodePage(env.newCollation, stateObj);
30843083

30853084
if (env.newCollation.IsUTF8)
30863085
{ // UTF8 collation
30873086
_defaultEncoding = Encoding.UTF8;
3088-
3089-
if (newCodePage != _defaultCodePage)
3090-
{
3091-
_defaultCodePage = newCodePage;
3092-
}
30933087
}
30943088
else
30953089
{
3096-
3090+
int newCodePage = GetCodePage(env.newCollation, stateObj);
30973091
if (newCodePage != _defaultCodePage)
30983092
{
30993093
_defaultCodePage = newCodePage;

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

Lines changed: 62 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;
6+
using System.Text;
57
using Xunit;
68

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

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

0 commit comments

Comments
 (0)