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


C# SqlConnection.Create方法代码示例

本文整理汇总了C#中System.Data.SqlClient.SqlConnection.Create方法的典型用法代码示例。如果您正苦于以下问题:C# SqlConnection.Create方法的具体用法?C# SqlConnection.Create怎么用?C# SqlConnection.Create使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Data.SqlClient.SqlConnection的用法示例。


在下文中一共展示了SqlConnection.Create方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: insert_with_fk_on_existing_special_child_inserts_parent_and_not_child

        public void insert_with_fk_on_existing_special_child_inserts_parent_and_not_child()
        {
            var logger = CreateMockLogger();

            var newDto = new ParentDto()
            {
                OneToOneSpecialChildDtoWithFk = new OneToOneSpecialChildDtoWithFk
                {
                    ChildKey = 100,
                    Name = "You, sir, are drunk!"
                }
            };

            try
            {
                using (var connection = new SqlConnection())
                {
                    connection.Create(newDto);
                }
            }
            catch (InvalidOperationException)
            {
                // Do nothing because we deliberately didn't open the connection
            }

            var scripts = logger.Scripts;
            Assert.AreEqual(2, scripts.Count, "Unexpected number of scripts.");

            var sql = scripts[0].Buffer.ToString();
            Assert.IsTrue(sql.Contains("INSERT INTO dbo.[Parent]"), "No INSERT on parent.");

            sql = scripts[1].Buffer.ToString();
            Assert.IsTrue(sql.Contains("UPDATE dbo.OneToOneSpecialChildWithFk"), "Should be an UPDATE on child.");
        }
开发者ID:rodrigoferrobrti,项目名称:Dapper.SimpleSave,代码行数:34,代码来源:ContextualReferenceDataTests.cs

示例2: insert_contact_referencing_phone_numbers_sets_guid_values_not_ints_on_fk_columns

        public void insert_contact_referencing_phone_numbers_sets_guid_values_not_ints_on_fk_columns()
        {
            var logger = new MockSimpleSaveLogger();
            SimpleSaveExtensions.Logger = logger;

            try
            {
                using (var connection = new SqlConnection())
                {
                    connection.Create(SamplePopulatedDtos.Contact);
                }
            }
            catch (InvalidOperationException)
            {
                //  Not a real DB connection.
            }

            var scripts = logger.Scripts;
            Assert.AreEqual(1, scripts.Count, "Unexpected number of scripts.");

            var script = scripts[0];

            var guid = ExtractGuid(script, "p7");
            Assert.AreEqual(
                SamplePopulatedDtos.Contact.MainPhone.PhoneGuid,
                guid,
                string.Format("Invalid main phone GUID: {0}", guid));

            guid = ExtractGuid(script, "p8");
            Assert.AreEqual(
                SamplePopulatedDtos.Contact.MobilePhone.PhoneGuid,
                guid,
                string.Format("Invalid main phone GUID: {0}", guid));
        }
开发者ID:rodrigoferrobrti,项目名称:Dapper.SimpleSave,代码行数:34,代码来源:ForeignKeyReferenceNonPKColumnTests.cs

示例3: user_assigned_primary_key_is_included_in_insert

        public void user_assigned_primary_key_is_included_in_insert()
        {
            var dto = new AuthorisationFee
            {
                FieldItemKey = 4096,
                Value = 1000,
                Revenue = 100,
                Cost = 1500,
                CalculatorVersionKey = 23,
                Name = "Dr Doolittle"
            };

            var logger = CreateMockLogger();
            try
            {
                using (var connection = new SqlConnection())
                {
                    connection.Create(dto);
                }
            }
            catch (InvalidOperationException ioe)
            {
                Assert.IsTrue(ioe.Message.Contains(
                    "Invalid operation. The connection is closed."),
                    string.Format("Right type of exception, wrong message: {0}\r\n{1}", ioe.Message, ioe.StackTrace));
            }

            var scripts = logger.Scripts;
            Assert.AreEqual(1, scripts.Count, "Unexpected number of scripts.");

            var sql = scripts[0].Buffer.ToString();
            Assert.IsTrue(sql.Contains("FieldItemKey"), "Should insert into PK column.");
            Assert.IsFalse(sql.Contains("SELECT SCOPE_IDENTITY();"), "Should not return PK value.");
        }
开发者ID:rodrigoferrobrti,项目名称:Dapper.SimpleSave,代码行数:34,代码来源:NonDbAssignedPrimaryKeyTests.cs

示例4: insert_application_with_nested_children_inserts_children

        public void insert_application_with_nested_children_inserts_children()
        {
            var application = CreateTestApplication();
            var logger = new MockSimpleSaveLogger();
            SimpleSaveExtensions.Logger = logger;

            try
            {
                using (IDbConnection connection = new SqlConnection())
                {
                    connection.Create(application);
                }
            }
            catch (InvalidOperationException)
            {
                //  Don't care
            }

            var scripts = logger.Scripts;
            Assert.AreEqual(5, scripts.Count, "Unexpected number of scripts.");

            Assert.IsTrue(scripts[0].Buffer.ToString().Contains("INSERT INTO [app].[APPLICATION_MST]"));
            Assert.IsTrue(scripts[1].Buffer.ToString().Contains("INSERT INTO [app].[LEGAL_INFO_MST]"));
            //  Address has to go in first or it can't be referenced, which is where the problem lies
            Assert.IsTrue(scripts[2].Buffer.ToString().Contains("INSERT INTO [gen].[ADDRESS_MST]"));
            Assert.IsTrue(scripts[3].Buffer.ToString().Contains("INSERT INTO [app].[LOCATION_MST]"));
            Assert.IsTrue(scripts[4].Buffer.ToString().Contains("INSERT INTO [opp].[OPPORTUNITY_MST]"));
        }
开发者ID:Paymentsense,项目名称:Dapper.SimpleSave,代码行数:28,代码来源:NestedObjectScriptGenerationTests.cs

示例5: throws_on_insert_with_duplicate_property_mappings_by_default

 public void throws_on_insert_with_duplicate_property_mappings_by_default()
 {
     var newObject = new TableB() { Name = "Captain Jack Sparrow" };
     using (IDbConnection connection = new SqlConnection())
     {
         connection.Create(newObject);
     }
 }
开发者ID:Paymentsense,项目名称:Dapper.SimpleSave,代码行数:8,代码来源:MultiplePropertiesSameColumnTests.cs

示例6: insert_offer_with_gateway_offer_should_only_insert_offer

        public void insert_offer_with_gateway_offer_should_only_insert_offer()
        {
            var offer = new OfferDto
            {
                GatewayOffer = new GatewayOfferDto
                {
                    IsBilledYearly = true,
                    PeriodicCharge = 100
                },
                OfferReference = "Test offer",
                OpportunityGuid = Guid.NewGuid()
            };

            var logger = new MockSimpleSaveLogger();
            SimpleSaveExtensions.Logger = logger;

            try
            {
                using (IDbConnection connection = new SqlConnection())
                {
                    connection.Create(offer);
                }
            }
            catch (InvalidOperationException)
            {
                //  Don't care
            }

            var scripts = logger.Scripts;
            Assert.AreEqual(1, scripts.Count, "Unexpected number of scripts.");

            var sql = scripts[0].Buffer.ToString();

            Assert.IsTrue(
                sql.Contains("INSERT INTO [opp].[OFFER_TRN]"),
                "Should insert into OFFER_TRN.");

            Assert.IsFalse(
                sql.Contains("[opp].[GATEWAY_OFFER_TRN]"),
                "Should not insert into GATEWAY_OFFER_TRN");
        }
开发者ID:Paymentsense,项目名称:Dapper.SimpleSave,代码行数:41,代码来源:NestedObjectScriptGenerationTests.cs


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