本文整理汇总了C#中System.Db.Initialize方法的典型用法代码示例。如果您正苦于以下问题:C# Db.Initialize方法的具体用法?C# Db.Initialize怎么用?C# Db.Initialize使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Db
的用法示例。
在下文中一共展示了Db.Initialize方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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");
}
示例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");
}
示例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);
}
示例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");
}
示例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()));
}
示例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");
}
示例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");
}
示例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");
}
示例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");
}
示例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);
}
示例11: Each_Db_Have_Own_Connection
public void Each_Db_Have_Own_Connection()
{
var db = new Db();
var db2 = new Db();
db.Initialize(TestConstants.SqlClientConnectionString, "System.Data.SqlClient", ConnectionStrategyType.Manual);
db2.Initialize(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\romag\Documents\Visual Studio 2015\Projects\EasyDb\EasyDbTests\NorthWind.mdb", "System.Data.OleDb");
db.Open();
Assert.AreEqual(db.Connection.ConnectionString, TestConstants.SqlClientConnectionString);
Assert.AreEqual(db.Connection.Provider, "System.Data.SqlClient");
Assert.AreEqual(db.Connection.ConnectionStrategy, ConnectionStrategyType.Manual);
Assert.AreEqual(db.Connection.State, System.Data.ConnectionState.Open);
Assert.AreEqual(db2.Connection.ConnectionString, @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\romag\Documents\Visual Studio 2015\Projects\EasyDb\EasyDbTests\NorthWind.mdb");
Assert.AreEqual(db2.Connection.Provider, "System.Data.OleDb");
Assert.AreEqual(db2.Connection.ConnectionStrategy, ConnectionStrategyType.Automatic);
Assert.AreEqual(db2.Connection.State, System.Data.ConnectionState.Closed);
db.Close();
}
示例12: 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");
}