当前位置: 首页>>代码示例>>C#>>正文


C# SqlConnection.ChangeDatabase方法代码示例

本文整理汇总了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;
                    }
                }
            }
        }
开发者ID:SGuyGe,项目名称:corefx,代码行数:75,代码来源:WeakRefTest.cs

示例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();
            }
        }
开发者ID:SGuyGe,项目名称:corefx,代码行数:88,代码来源:WeakRefTestYukonSpecific.cs


注:本文中的SqlConnection.ChangeDatabase方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。