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


C# Db.CreateCommand方法代码示例

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


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

示例1: Relations_Zero_One_And_One_One_Async

        public async Task Relations_Zero_One_And_One_One_Async()
        {
            var db = new Db();
            db.Initialize(TestConstants.SqlClientConnectionString, "System.Data.SqlClient");

            var result = await db.CreateCommand("select * from [dbo].[BaseTable] where [email protected]")
                .AddParameter("@id", 5)
                .ReadOneMapToAsync<BaseTable>();

            // zero one
            if (result.ZeroOneId.HasValue)
            {
                var zeroOne = await db.CreateCommand("Select * from [dbo].[ZeroOne] where [email protected]")
                  .AddParameter("@id", result.ZeroOneId)
                  .ReadOneMapToAsync<ZeroOne>();

                result.ZeroOne = zeroOne;
            }

            // one one
            var oneOne = await db.CreateCommand("Select * from [dbo].[OneOne] where [email protected]")
                .AddParameter("@id", result.OneOneId)
                .ReadOneMapToAsync<OneOne>();

            result.OneOne = oneOne;

            Assert.AreEqual(result.Name.TrimEnd(), "Base 2");
            Assert.IsTrue(result.ZeroOneId.HasValue && result.ZeroOneId == 1);
            Assert.AreEqual(result.ZeroOne.Id, 1);
            Assert.AreEqual(result.ZeroOne.Name, "Zero 1");
            Assert.AreEqual(result.OneOneId, 3);
            Assert.AreEqual(result.OneOne.Id, 3);
            Assert.AreEqual(result.OneOne.Name, "One 2");
        }
开发者ID:romagny13,项目名称:EasyDb,代码行数:34,代码来源:RelationsTests.cs

示例2: Relations_Zero_One_And_One_One

        public void Relations_Zero_One_And_One_One()
        {
            var db = new Db();
            db.Initialize(TestConstants.SqlClientConnectionString, "System.Data.SqlClient");

            var result = db.CreateCommand("select * from [dbo].[BaseTable] where [email protected]")
                .AddParameter("@id", 4)
                .ReadOneMapTo<BaseTable>();

            // zero one
            if (result.ZeroOneId.HasValue)
            {
                var zeroOne = db.CreateCommand("Select * from [dbo].[ZeroOne] where [email protected]")
                  .AddParameter("@id", result.ZeroOneId)
                  .ReadOneMapTo<ZeroOne>();

                result.ZeroOne = zeroOne;
            }

            // one one
            var oneOne = db.CreateCommand("Select * from [dbo].[OneOne] where [email protected]")
                .AddParameter("@id", result.OneOneId)
                .ReadOneMapTo<OneOne>();

            result.OneOne = oneOne;

            Assert.AreEqual(result.Name.TrimEnd(), "Base 1");
            Assert.IsFalse(result.ZeroOneId.HasValue);
            Assert.AreEqual(result.OneOneId,2);
            Assert.AreEqual(result.OneOne.Id, 2);
            Assert.AreEqual(result.OneOne.Name, "One 1");
        }
开发者ID:romagny13,项目名称:EasyDb,代码行数:32,代码来源:RelationsTests.cs

示例3: Should_Connect_With_OleDb

        public void Should_Connect_With_OleDb()
        {
            var db = new Db();
            db.Initialize(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\romag\Documents\Visual Studio 2015\Projects\EasyDb\EasyDbTests\NorthWind.mdb", "System.Data.OleDb");

            var count = db.CreateCommand("select count(*) from Categories")
                .Scalar();

            var result = Helper.TryParseToInt(count);
            Assert.IsTrue(result.HasValue && result > 0);
        }
开发者ID:romagny13,项目名称:EasyDb,代码行数:11,代码来源:ProviderTests.cs

示例4: Should_Map_One_With_Guid

        public void Should_Map_One_With_Guid()
        {
            var db = new Db();
            db.Initialize(TestConstants.SqlClientConnectionString, "System.Data.SqlClient");

            var user = db.CreateCommand("select * from [dbo].[UsersWithGuid] where [email protected]")
                .AddParameter("@id", "39544cae-1342-4ab4-9d3c-921ffd93ac62")
                .ReadOneMapTo<UserWithGuid>();

            Assert.AreEqual(user.UserName, "Marie");
        }
开发者ID:romagny13,项目名称:EasyDb,代码行数:11,代码来源:TableWithGuidTests.cs

示例5: Should_Get_New_Guid_Key

        public void Should_Get_New_Guid_Key()
        {
            var db = new Db();
            db.Initialize(TestConstants.SqlClientConnectionString, "System.Data.SqlClient");

            var newGuid = db.CreateCommand("insert into [dbo].[UsersWithGuid](Name,Email) output INSERTED.Id values(@name,@email)")
                           .AddParameter("@name", "user1")
                           .AddParameter("@email", "[email protected]")
                           .Scalar();

            Assert.IsFalse(string.IsNullOrEmpty(newGuid.ToString()));
        }
开发者ID:romagny13,项目名称:EasyDb,代码行数:12,代码来源:TableWithGuidTests.cs

示例6: Should_Read_One_With_Guid

        public void Should_Read_One_With_Guid()
        {
            var db = new Db();
            db.Initialize(TestConstants.SqlClientConnectionString, "System.Data.SqlClient");

            var user = db.CreateCommand("select * from [dbo].[UsersWithGuid] where [email protected]")
                .AddParameter("@id", "39544cae-1342-4ab4-9d3c-921ffd93ac62")
                .ReadOne<UserWithGuid>((reader) =>
                {
                    return new UserWithGuid
                    {
                        Id = (Guid)reader["Id"],
                        UserName = (string)reader["Name"]
                    };
                });

            Assert.AreEqual(user.UserName.TrimEnd(), "Marie");
        }
开发者ID:romagny13,项目名称:EasyDb,代码行数:18,代码来源:TableWithGuidTests.cs

示例7: Should_Connect_With_SqlClient

        public void Should_Connect_With_SqlClient()
        {
            var db = new Db();
            db.Initialize(TestConstants.SqlClientConnectionString, "System.Data.SqlClient");

            var user = db.CreateCommand("select * from [dbo].[Users] where [email protected]")
                .AddParameter("@id", 11)
                .ReadOneMapTo<User>();

            Assert.AreEqual(user.UserName, "Marie");
        }
开发者ID:romagny13,项目名称:EasyDb,代码行数:11,代码来源:ProviderTests.cs

示例8: Should_Get_Data_With_OleDb

        public void Should_Get_Data_With_OleDb()
        {
            var db = new Db();
            db.Initialize(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\romag\Documents\Visual Studio 2015\Projects\EasyDb\EasyDbTests\NorthWind.mdb", "System.Data.OleDb");

            var category = db.CreateCommand("select * from Categories where [email protected]")
                .AddParameter("@id",1)
                .ReadOneMapTo<Category>();

            Assert.AreEqual(category.CategoryName.Trim(), "Beverages");
        }
开发者ID:romagny13,项目名称:EasyDb,代码行数:11,代码来源:ProviderTests.cs

示例9: Should_Get_Data_With_MySQL

        public void Should_Get_Data_With_MySQL()
        {
            var db = new Db();
            db.Initialize(@"server=localhost;user id=root;database=mydb", "MySql.Data.MySqlClient");

            var user = db.CreateCommand("select * from users where [email protected]")
                .AddParameter("@id", 1)
                .ReadOneMapTo<MySQLUser>();

            Assert.AreEqual(user.UserName.Trim(), "Marie");
        }
开发者ID:romagny13,项目名称:EasyDb,代码行数:11,代码来源:ProviderTests.cs

示例10: Cannot_Execute_Command_With_Manual_Strategy_And_Connection_Closed

        public void Cannot_Execute_Command_With_Manual_Strategy_And_Connection_Closed()
        {
            bool fail = false;
            var db = new Db();
            db.Initialize(TestConstants.SqlClientConnectionString, "System.Data.SqlClient",ConnectionStrategyType.Manual);

            try
            {
                var users = db.CreateCommand("select * from [dbo].[Users]")
                              .ReadAllMapTo<User>();
            }
            catch (Exception)
            {
                fail = true;
            }
          
            Assert.IsTrue(fail);
        }
开发者ID:romagny13,项目名称:EasyDb,代码行数:18,代码来源:ReadWriteTests.cs

示例11: Relations_Many_Many

        public void Relations_Many_Many()
        {
            var db = new Db();
            db.Initialize(TestConstants.SqlClientConnectionString, "System.Data.SqlClient");

            var result = db.CreateCommand("select * from [dbo].[BaseTable] where [email protected]")
                .AddParameter("@id", 4)
                .ReadOneMapTo<BaseTable>();

            // Many many
            var manyManyList = db.CreateCommand("select * from [dbo].[ManyMany] where [email protected]")
                .AddParameter("@id", 4)
                .ReadAllMapTo<ManyMany>();

            var manyTablesList = new List<ManyTable>();
            foreach (var many in manyManyList)
            {
                var manyTable = db.CreateCommand("select * from [dbo].[ManyTable] where [email protected]")
                    .AddParameter("@id", many.ManyTable_Id)
                    .ReadOneMapTo<ManyTable>();

                manyTablesList.Add(manyTable);
            }
            result.ManyList = manyTablesList;

            Assert.AreEqual(result.ManyList[0].Id, 1);
            Assert.AreEqual(result.ManyList[0].Name, "Many 1");
            Assert.AreEqual(result.ManyList[1].Id, 2);
            Assert.AreEqual(result.ManyList[1].Name, "Many 2");
        }
开发者ID:romagny13,项目名称:EasyDb,代码行数:30,代码来源:RelationsTests.cs


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