Skip to content

Commit 711b72c

Browse files
author
Javad
authored
[5.0.1] Fix | Default UTF8 collation conflict (#1799)
1 parent 25f2d0b commit 711b72c

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
@@ -3136,21 +3136,14 @@ private bool TryProcessEnvChange(int tokenLength, TdsParserStateObject stateObj,
31363136
_defaultCollation = env._newCollation;
31373137
_defaultLCID = env._newCollation.LCID;
31383138

3139-
int newCodePage = GetCodePage(env._newCollation, stateObj);
3140-
31413139
// UTF8 collation
31423140
if (env._newCollation.IsUTF8)
31433141
{
31443142
_defaultEncoding = Encoding.UTF8;
3145-
3146-
if (newCodePage != _defaultCodePage)
3147-
{
3148-
_defaultCodePage = newCodePage;
3149-
}
31503143
}
31513144
else
31523145
{
3153-
3146+
int newCodePage = GetCodePage(env._newCollation, stateObj);
31543147
if (newCodePage != _defaultCodePage)
31553148
{
31563149
_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;
6+
using System.Text;
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)