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


C# SqlConnection.ExecuteStoredProcedure方法代码示例

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


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

示例1: StoredProcedure_WithSqlTransactionCommitted_DoesWriteRecords

        public void StoredProcedure_WithSqlTransactionCommitted_DoesWriteRecords()
        {
            // ARRANGE
            const int expectedIntermediateCount = 5;
            int originalCount;
            int intermediateCount;
            int finalCount;
            string connectionString = Settings.Default.ExampleDatabaseConnection;
            var companiesToAdd = new List<CompaniesAdd.CompanyTableType>
            {
                new CompaniesAdd.CompanyTableType { CompanyName = "Company 1", IsActive = true, TenantId = 2 },
                new CompaniesAdd.CompanyTableType { CompanyName = "Company 2", IsActive = false, TenantId = 2 },
                new CompaniesAdd.CompanyTableType { CompanyName = "Company 3", IsActive = true, TenantId = 2 }
            };
            var companiesAddParameters = new CompaniesAdd.CompaniesAddParameters
            {
                Companies = companiesToAdd
            };
            var companyAddProcedure = new CompaniesAdd(companiesAddParameters);
            var companyCountProcedure = new CompanyCountAll();
            var companyDeleteParameters = new TenantIdParameters
            {
                TenantId = 2
            };
            var companyDeleteProcedure = new CompanyDeleteForTenantId(companyDeleteParameters);

            // ACT

            using (var connection = new SqlConnection(connectionString))
            {
                connection.Open();
                SqlTransaction transaction;
                using (transaction = connection.BeginTransaction())
                {
                    originalCount = connection.ExecuteStoredProcedure(companyCountProcedure, transaction: transaction).First().CountOfCompanies;
                    connection.ExecuteStoredProcedure(companyAddProcedure, transaction: transaction);
                    transaction.Commit();
                }
            }

            using (var connection = new SqlConnection(connectionString))
            {
                connection.Open();
                intermediateCount = connection.ExecuteStoredProcedure(companyCountProcedure).First().CountOfCompanies;
                connection.ExecuteStoredProcedure(companyDeleteProcedure);
                finalCount = connection.ExecuteStoredProcedure(companyCountProcedure).First().CountOfCompanies;
                connection.Close();
            }

            // ASSERT
            Assert.AreEqual(originalCount, finalCount);
            Assert.AreEqual(expectedIntermediateCount, intermediateCount);
        }
开发者ID:dibley1973,项目名称:StoredProcedureFramework,代码行数:53,代码来源:StoredProcedureWithTransactions.cs

示例2: StoredProcedure_WithSqlTransactionCommitted_DoesWriteRecords

        public void StoredProcedure_WithSqlTransactionCommitted_DoesWriteRecords()
        {
            // ARRANGE
            const int expectedIntermediateCount = 3;
            int originalCount;
            int intermediateCount;
            int finalCount;
            string connectionString = Settings.Default.IntegrationTestConnection;
            var itemsToAdd = new List<TransactionTestParameterTableType>
            {
                new TransactionTestParameterTableType { Name = "Company 1", IsActive = true, Id = 1 },
                new TransactionTestParameterTableType { Name = "Company 2", IsActive = false, Id = 2 },
                new TransactionTestParameterTableType { Name = "Company 3", IsActive = true, Id = 3 }
            };
            var transactionAddParameters = new TransactionTestAddStoredProcedure.Parameter
            {
                TvpParameters = itemsToAdd
            };
            var transactionTestAddProcedure = new TransactionTestAddStoredProcedure(transactionAddParameters);
            var transactionTestCountProcedure = new TransactionTestCountAllStoredProcedure();
            var transactionDeleteProcedure = new TransactionTestDeleteAllStoredProcedure();

            // ACT

            using (var connection = new SqlConnection(connectionString))
            {
                connection.Open();
                SqlTransaction transaction;
                using (transaction = connection.BeginTransaction())
                {
                    originalCount = connection.ExecuteStoredProcedure(transactionTestCountProcedure, transaction: transaction).First().Count;
                    connection.ExecuteStoredProcedure(transactionTestAddProcedure, transaction: transaction);
                    transaction.Commit();
                }
            }

            using (var connection = new SqlConnection(connectionString))
            {
                connection.Open();
                intermediateCount = connection.ExecuteStoredProcedure(transactionTestCountProcedure).First().Count;
                connection.ExecuteStoredProcedure(transactionDeleteProcedure);
                finalCount = connection.ExecuteStoredProcedure(transactionTestCountProcedure).First().Count;
                connection.Close();
            }

            // ASSERT
            Assert.AreEqual(originalCount, finalCount);
            Assert.AreEqual(expectedIntermediateCount, intermediateCount);
        }
开发者ID:dibley1973,项目名称:StoredProcedureFramework,代码行数:49,代码来源:StoredProcedureWithTransactions.cs

示例3: NormalStoredProcedure_WhenCalledOnSqlConnection_ReturnsCorrectValues

        public void NormalStoredProcedure_WhenCalledOnSqlConnection_ReturnsCorrectValues()
        {
            // ARRANGE
            const int expectedId = 10;
            const string expectedName = @"Dave";
            const bool expectedActive = true;

            var parameters = new NormalStoredProcedureParameters
            {
                Id = expectedId
            };
            List<NormalStoredProcedureReturnType> results;
            var procedure = new NormalStoredProcedure(parameters);
            var connectionString = ConfigurationManager.ConnectionStrings["IntegrationTestConnection"].ConnectionString;

            // ACT
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();
                results = connection.ExecuteStoredProcedure(procedure);
            }
            var result = results.First();

            // ASSERT
            Assert.AreEqual(expectedId, result.Id);
            Assert.AreEqual(expectedName, result.Name);
            Assert.AreEqual(expectedActive, result.Active);
        }
开发者ID:aprishchepov,项目名称:StoredProcedureFramework,代码行数:28,代码来源:MostBasicStoredProcedureTests.cs

示例4: ExecuteStoredProcedure_WithNullStoredProcedure_ThrowsArgumentNullException

        public void ExecuteStoredProcedure_WithNullStoredProcedure_ThrowsArgumentNullException()
        {
            // ARRANGE
            MostBasicStoredProcedure procedure = null;
            var connectionString = ConfigurationManager.ConnectionStrings["IntegrationTestConnection"].ConnectionString;

            // ACT
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();
                // ReSharper disable once ExpressionIsAlwaysNull
                connection.ExecuteStoredProcedure(procedure);
            }

            // ASSERT
            // Exception should have been thrown
        }
开发者ID:dibley1973,项目名称:StoredProcedureFramework,代码行数:17,代码来源:SqlConnectionExtensionsTests.cs

示例5: CompanyGetAllForTenantId

        public void CompanyGetAllForTenantId()
        {
            // ARRANGE
            const int expectedCompanyCount = 2;
            string connectionName = Settings.Default.ExampleDatabaseConnection;
            var parameters = new TenantIdParameters { TenantId = 1 };
            var procedure = new CompanyGetAllForTenantID(parameters);
            List<CompanyDto> companies;
            CompanyDto company1;

            // ACT
            using (SqlConnection connection = new SqlConnection(connectionName))
            {
                companies = connection.ExecuteStoredProcedure(procedure);
                company1 = companies.FirstOrDefault();
            }

            // ASSERT
            Assert.AreEqual(expectedCompanyCount, companies.Count);
            Assert.IsNotNull(company1);
        }
开发者ID:dibley1973,项目名称:StoredProcedureFramework,代码行数:21,代码来源:StoredProcedureFromSqlConnection.cs

示例6: StoredProcedure_WithTransactionScopeNotCommited_DoesNotWriteRecords

        public void StoredProcedure_WithTransactionScopeNotCommited_DoesNotWriteRecords()
        {
            // ARRANGE
            const int expectedIntermediateCount = 5;
            int originalCount;
            int intermediateCount;
            int finalCount;
            string connectionString = Settings.Default.ExampleDatabaseConnection;
            var companiesToAdd = new List<CompaniesAdd.CompanyTableType>
            {
                new CompaniesAdd.CompanyTableType { CompanyName = "Company 1", IsActive = true, TenantId = 2 },
                new CompaniesAdd.CompanyTableType { CompanyName = "Company 2", IsActive = false, TenantId = 2 },
                new CompaniesAdd.CompanyTableType { CompanyName = "Company 3", IsActive = true, TenantId = 2 }
            };
            var parameters = new CompaniesAdd.CompaniesAddParameters
            {
                Companies = companiesToAdd
            };
            var companyAddProcedure = new CompaniesAdd(parameters);
            var companyCountProcedure = new CompanyCountAll();

            // ACT
            using (new TransactionScope(TransactionScopeOption.RequiresNew))
            {
                using (var connection = new SqlConnection(connectionString))
                {
                    connection.Open();
                    originalCount = connection.ExecuteStoredProcedure(companyCountProcedure).First().CountOfCompanies;
                    connection.ExecuteStoredProcedure(companyAddProcedure);
                    intermediateCount = connection.ExecuteStoredProcedure(companyCountProcedure).First().CountOfCompanies;
                }
            }
            using (var connection = new SqlConnection(connectionString))
            {
                connection.Open();
                finalCount = connection.ExecuteStoredProcedure(companyCountProcedure).First().CountOfCompanies;
                connection.Close();
            }

            // ASSERT
            Assert.AreEqual(originalCount, finalCount);
            Assert.AreEqual(expectedIntermediateCount, intermediateCount);
        }
开发者ID:dibley1973,项目名称:StoredProcedureFramework,代码行数:43,代码来源:StoredProcedureWithTransactions.cs


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