本文整理汇总了C#中SqlConnection.ChangeDatabase方法的典型用法代码示例。如果您正苦于以下问题:C# SqlConnection.ChangeDatabase方法的具体用法?C# SqlConnection.ChangeDatabase怎么用?C# SqlConnection.ChangeDatabase使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SqlConnection
的用法示例。
在下文中一共展示了SqlConnection.ChangeDatabase方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestReaderNonMarsCase
private static void TestReaderNonMarsCase(string caseName, string connectionString, ReaderTestType testType, ReaderVerificationType verificationType)
{
WeakReference weak = null;
using (SqlConnection con = new SqlConnection(connectionString))
{
con.Open();
using (SqlCommand cmd = con.CreateCommand())
{
cmd.CommandText = COMMAND_TEXT_1;
SqlDataReader gch = null;
if ((testType != ReaderTestType.ReaderGC) && (testType != ReaderTestType.ReaderGCConnectionClose))
gch = cmd.ExecuteReader();
switch (testType)
{
case ReaderTestType.ReaderClose:
gch.Dispose();
break;
case ReaderTestType.ReaderDispose:
gch.Dispose();
break;
case ReaderTestType.ReaderGC:
weak = OpenNullifyReader(cmd);
GC.Collect();
GC.WaitForPendingFinalizers();
Assert.False(weak.IsAlive, "Reader is still alive!");
break;
case ReaderTestType.ConnectionClose:
GC.SuppressFinalize(gch);
con.Close();
con.Open();
break;
case ReaderTestType.ReaderGCConnectionClose:
weak = OpenNullifyReader(cmd);
GC.Collect();
GC.WaitForPendingFinalizers();
Assert.False(weak.IsAlive, "Reader is still alive!");
con.Close();
con.Open();
break;
}
switch (verificationType)
{
case ReaderVerificationType.ExecuteReader:
cmd.CommandText = COMMAND_TEXT_2;
using (SqlDataReader rdr = cmd.ExecuteReader())
{
rdr.Read();
Assert.Equal(rdr.FieldCount, 1);
Assert.Equal(rdr.GetName(0), COLUMN_NAME_2);
}
break;
case ReaderVerificationType.ChangeDatabase:
con.ChangeDatabase(DATABASE_NAME);
Assert.Equal(con.Database, DATABASE_NAME);
break;
case ReaderVerificationType.BeginTransaction:
cmd.Transaction = con.BeginTransaction();
cmd.CommandText = "select @@trancount";
int tranCount = (int)cmd.ExecuteScalar();
Assert.Equal(tranCount, 1);
break;
}
}
}
}
示例2: TestReaderMarsCase
private static void TestReaderMarsCase(string caseName, string connectionString, ReaderTestType testType, ReaderVerificationType verificationType)
{
WeakReference weak = null;
SqlCommand[] cmd = new SqlCommand[CONCURRENT_COMMANDS];
SqlDataReader[] gch = new SqlDataReader[CONCURRENT_COMMANDS];
using (SqlConnection con = new SqlConnection(connectionString))
{
con.Open();
for (int i = 0; i < CONCURRENT_COMMANDS; i++)
{
cmd[i] = con.CreateCommand();
cmd[i].CommandText = COMMAND_TEXT_1;
if ((testType != ReaderTestType.ReaderGC) && (testType != ReaderTestType.ReaderGCConnectionClose))
gch[i] = cmd[i].ExecuteReader();
else
gch[i] = null;
}
for (int i = 0; i < CONCURRENT_COMMANDS; i++)
{
switch (testType)
{
case ReaderTestType.ReaderClose:
gch[i].Dispose();
break;
case ReaderTestType.ReaderDispose:
gch[i].Dispose();
break;
case ReaderTestType.ReaderGC:
weak = OpenNullifyReader(cmd[i]);
GC.Collect();
GC.WaitForPendingFinalizers();
Assert.False(weak.IsAlive, "Transaction is still alive on TestReaderMars: ReaderGC");
break;
case ReaderTestType.ConnectionClose:
GC.SuppressFinalize(gch[i]);
con.Close();
con.Open();
break;
case ReaderTestType.ReaderGCConnectionClose:
weak = OpenNullifyReader(cmd[i]);
GC.Collect();
GC.WaitForPendingFinalizers();
Assert.False(weak.IsAlive, "Transaction is still alive on TestReaderMars: ReaderGCConnectionClose");
con.Close();
con.Open();
break;
}
cmd[i].Dispose();
}
SqlCommand verificationCmd = con.CreateCommand();
switch (verificationType)
{
case ReaderVerificationType.ExecuteReader:
verificationCmd.CommandText = COMMAND_TEXT_2;
using (SqlDataReader rdr = verificationCmd.ExecuteReader())
{
rdr.Read();
DataTestClass.AssertEqualsWithDescription(1, rdr.FieldCount, "Execute Reader should return expected Field count");
DataTestClass.AssertEqualsWithDescription(COLUMN_NAME_2, rdr.GetName(0), "Execute Reader should return expected Field name");
}
break;
case ReaderVerificationType.ChangeDatabase:
con.ChangeDatabase(DATABASE_NAME);
DataTestClass.AssertEqualsWithDescription(DATABASE_NAME, con.Database, "Change Database should return expected Database Name");
break;
case ReaderVerificationType.BeginTransaction:
verificationCmd.Transaction = con.BeginTransaction();
verificationCmd.CommandText = "select @@trancount";
int tranCount = (int)verificationCmd.ExecuteScalar();
DataTestClass.AssertEqualsWithDescription(1, tranCount, "Begin Transaction should return expected Transaction count");
break;
}
verificationCmd.Dispose();
}
}