本文整理汇总了C#中OrmLiteConnectionFactory类的典型用法代码示例。如果您正苦于以下问题:C# OrmLiteConnectionFactory类的具体用法?C# OrmLiteConnectionFactory怎么用?C# OrmLiteConnectionFactory使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
OrmLiteConnectionFactory类属于命名空间,在下文中一共展示了OrmLiteConnectionFactory类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C#代码示例。
示例1: Simple_CRUD_example
public void Simple_CRUD_example()
{
var path = Config.SqliteFileDb;
if (File.Exists(path))
File.Delete(path);
var connectionFactory = new OrmLiteConnectionFactory(path, SqliteOrmLiteDialectProvider.Instance.WithPassword("bob"));
using (var db = connectionFactory.OpenDbConnection())
{
db.CreateTable<User>(true);
db.Insert(new User {Id = 1, Name = "A", CreatedDate = DateTime.Now});
db.Insert(new User {Id = 2, Name = "B", CreatedDate = DateTime.Now});
db.Insert(new User {Id = 3, Name = "B", CreatedDate = DateTime.Now});
var rowsB = db.Select<User>("Name = {0}", "B");
var rowsB1 = db.Select<User>(user => user.Name == "B");
Assert.That(rowsB, Has.Count.EqualTo(2));
Assert.That(rowsB1, Has.Count.EqualTo(2));
var rowIds = rowsB.ConvertAll(x => x.Id);
Assert.That(rowIds, Is.EquivalentTo(new List<long> {2, 3}));
rowsB.ForEach(x => db.Delete(x));
rowsB = db.Select<User>("Name = {0}", "B");
Assert.That(rowsB, Has.Count.EqualTo(0));
var rowsLeft = db.Select<User>();
Assert.That(rowsLeft, Has.Count.EqualTo(1));
Assert.That(rowsLeft[0].Name, Is.EqualTo("A"));
}
File.Delete(path);
}
开发者ID:ciker,项目名称:simplestack.ormlite,代码行数:35,代码来源:PasswordUseCase.cs
示例2: SaveDateTimeToDatabase
public void SaveDateTimeToDatabase()
{
var dbFactory = new OrmLiteConnectionFactory(base.ConnectionString, SqlServerOrmLiteDialectProvider.Instance);
SqlServerOrmLiteDialectProvider.Instance.EnsureUtc(true);
using (var db = dbFactory.OpenDbConnection())
{
var dateTime = new DateTime(2012, 1, 1, 1, 1, 1, DateTimeKind.Local);
var x = InsertAndSelectDateTime(db, dateTime);
Assert.AreEqual(DateTimeKind.Utc, x.Test.Kind);
Assert.AreEqual(x.Test.ToUniversalTime(), dateTime.ToUniversalTime());
Assert.AreEqual(x.Test.ToLocalTime(), dateTime.ToLocalTime());
dateTime = new DateTime(2012, 1, 1, 1, 1, 1, DateTimeKind.Utc);
x = InsertAndSelectDateTime(db, dateTime);
Assert.AreEqual(DateTimeKind.Utc, x.Test.Kind);
Assert.AreEqual(x.Test.ToUniversalTime(), dateTime.ToUniversalTime());
Assert.AreEqual(x.Test.ToLocalTime(), dateTime.ToLocalTime());
dateTime = new DateTime(2012, 1, 1, 1, 1, 1, DateTimeKind.Unspecified);
x = InsertAndSelectDateTime(db, dateTime);
Assert.AreEqual(DateTimeKind.Utc, x.Test.Kind);
Assert.AreEqual(x.Test.ToUniversalTime(), dateTime);
Assert.AreEqual(x.Test.ToLocalTime(), dateTime.ToLocalTime());
}
}
开发者ID:yeurch,项目名称:ServiceStack.OrmLite,代码行数:26,代码来源:EnsureUtcTest.cs
示例3: datetime_tests__check_default_behaviour
public void datetime_tests__check_default_behaviour()
{
var dbFactory = new OrmLiteConnectionFactory(base.ConnectionString, SqlServerOrmLiteDialectProvider.Instance);
//default behaviour: normal datetime can't hold DateTime values of year 1.
(SqlServerOrmLiteDialectProvider.Instance as SqlServerOrmLiteDialectProvider).UseDatetime2(false);
using(var conn = dbFactory.OpenDbConnection()) {
var test_object_ValidForDatetime2 = Table_for_datetime2_tests.get_test_object_ValidForDatetime2();
var test_object_ValidForNormalDatetime = Table_for_datetime2_tests.get_test_object_ValidForNormalDatetime();
conn.CreateTable<Table_for_datetime2_tests>(true);
//normal insert
var insertedId = conn.Insert(test_object_ValidForNormalDatetime, selectIdentity:true);
//insert works, but can't regular datetime's precision is not great enough.
var fromDb = conn.SingleById<Table_for_datetime2_tests>(insertedId);
Assert.AreNotEqual(test_object_ValidForNormalDatetime.ToVerifyPrecision, fromDb.ToVerifyPrecision);
var thrown = Assert.Throws<SqlTypeException>(() => {
conn.Insert(test_object_ValidForDatetime2);
});
Assert.That(thrown.Message.Contains("SqlDateTime overflow"));
//check InsertParam
conn.Insert(test_object_ValidForNormalDatetime);
//InsertParam fails differently:
var insertParamException = Assert.Throws<System.Data.SqlTypes.SqlTypeException>(() => {
conn.Insert(test_object_ValidForDatetime2);
});
Assert.That(insertParamException.Message.Contains("SqlDateTime overflow"));
}
}
开发者ID:JackFong,项目名称:ServiceStack.OrmLite,代码行数:35,代码来源:Datetime2Tests.cs
示例4: Can_query_using_float_in_alernate_culuture
public void Can_query_using_float_in_alernate_culuture()
{
var dbFactory = new OrmLiteConnectionFactory(
GetFileConnectionString(),
FirebirdOrmLiteDialectProvider.Instance);
using (var db =dbFactory.OpenDbConnection())
using (var dbCmd = db.CreateCommand())
{
dbCmd.CreateTable<Point>(true);
dbCmd.Insert(new Point { Width = 4, Height = 1.123f, Top = 3.456d, Left = 2.345m});
var points = dbCmd.Select<Point>();
Console.WriteLine(points.Dump());
Assert.That(points[0].Width, Is.EqualTo(4));
Assert.That(points[0].Height, Is.EqualTo(1.123f));
Assert.That(points[0].Top, Is.EqualTo(3.456d));
Assert.That(points[0].Left, Is.EqualTo(2.345m));
points = dbCmd.Select<Point>("Height={0}", 1.123f); // returns no rows! FirebirdSql bug?
Assert.That(points.Count>0);
}
}
开发者ID:JduPreez,项目名称:ServiceStack.OrmLite,代码行数:31,代码来源:LocalizationTests.cs
示例5: test_if_ormlite_sql_prepare_update_works_for_image_blobs
public void test_if_ormlite_sql_prepare_update_works_for_image_blobs()
{
var dbFactory = new OrmLiteConnectionFactory(ConnectionString, SqlServerOrmLiteDialectProvider.Instance);
using (var db = dbFactory.OpenDbConnection())
using (var dbConn = db.CreateCommand())
{
dbConn.CreateTable<ImageBlobDto>(true);
var dto = new ImageBlobDto()
{
Image1 = ImageToBytes(ImageBlobResource.Bild),
Image2 = ImageToBytes(ImageBlobResource.Bild2),
Image3 = ImageToBytes(ImageBlobResource.Bild3),
Complex = new object[] { "Foo", "Bar", "Baz", 1 }
};
var provider = OrmLiteConfig.DialectProvider as OrmLiteDialectProviderBase;
Assert.NotNull(provider);
using (var insertCmd = db.CreateInsertStatement(dto))
{
var nInserted = insertCmd.ExecuteNonQuery();
Assert.Greater(nInserted, 0);
}
dto.Id = 1;
dto.Complex = new object[] { "Baz", "Bar", "Foo", 2 };
using (var updateCmd = db.CreateUpdateStatement(dto))
{
var nUpdated = updateCmd.ExecuteNonQuery();
Assert.Greater(nUpdated, 0);
}
}
}
开发者ID:JackFong,项目名称:ServiceStack.OrmLite,代码行数:35,代码来源:OrmLiteInsertImageBlobTests.cs
示例6: Can_use_nested_transactions
public void Can_use_nested_transactions()
{
//must use the factory, because that returns an OrmLiteConnection, that can saves the current transaction
var factory = new OrmLiteConnectionFactory(ConnectionString, SqlServerDialect.Provider);
//using(var outerConn = OpenDbConnection()) {
using(var outerConn = factory.OpenDbConnection()) {
//(re)create tables
outerConn.DropAndCreateTable<Can_use_nested_transactions_Table1>();
outerConn.DropAndCreateTable<Can_use_nested_transactions_Table2>();
//using(var innerConn = OpenDbConnection()) {//use the factory to get the connections
using(var innerConn = factory.OpenDbConnection()) {
using(var outerTran = outerConn.OpenTransaction()) {
outerConn.Insert(new Can_use_nested_transactions_Table1 { Dummy = DateTime.Now });
using(var innerTran = innerConn.OpenTransaction()) {
//The other transaction inserts into table1, Table2 is not locked
innerConn.Insert(new Can_use_nested_transactions_Table2 { Dummy = DateTime.Now });
//fails here, because innerTran has overwritten the ThreadStatic OrmLiteConfig.CurrentTransaction
outerConn.Insert(new Can_use_nested_transactions_Table1 { Dummy = DateTime.Now });
outerConn.Insert(new Can_use_nested_transactions_Table1 { Dummy = DateTime.Now });
}
}
}
}
}
开发者ID:sami1971,项目名称:NServiceKit.OrmLite,代码行数:29,代码来源:NestedTransactions.cs
示例7: Can_Delete_from_basic_persistence_provider
public void Can_Delete_from_basic_persistence_provider()
{
using (var db = new OrmLiteConnectionFactory(ConnectionString, FirebirdDialect.Provider).Open())
{
db.CreateTable<ModelWithFieldsOfDifferentTypes>(true);
var basicProvider = new OrmLitePersistenceProvider(db);
var rowIds = new List<int> {1, 2, 3, 4, 5};
var rows = rowIds.ConvertAll(x => ModelWithFieldsOfDifferentTypes.Create(x));
rows.ForEach(x => db.Insert(x));
var deleteRowIds = new List<int> {2, 4};
foreach (var row in rows)
{
if (deleteRowIds.Contains(row.Id))
{
basicProvider.Delete(row);
}
}
var providerRows = basicProvider.GetByIds<ModelWithFieldsOfDifferentTypes>(rowIds).ToList();
var providerRowIds = providerRows.ConvertAll(x => x.Id);
var remainingIds = new List<int>(rowIds);
deleteRowIds.ForEach(x => remainingIds.Remove(x));
Assert.That(providerRowIds, Is.EquivalentTo(remainingIds));
}
}
开发者ID:ciker,项目名称:simplestack.ormlite,代码行数:33,代码来源:OrmLiteBasicPersistenceProviderTest.cs
示例8: Can_get_data_from_TableWithNamigStrategy_with_query_by_example
public void Can_get_data_from_TableWithNamigStrategy_with_query_by_example()
{
OrmLite.OrmLiteConfig.DialectProvider.NamingStrategy =
OrmLite.OrmLiteConfig.DialectProvider.NamingStrategy = new PrefixNamingStrategy
{
TablePrefix = "tab_",
ColumnPrefix = "col_",
};
using (var db = new OrmLiteConnectionFactory(ConnectionString, FirebirdDialect.Provider).Open())
{
db.CreateTable<ModelWithOnlyStringFields>(true);
ModelWithOnlyStringFields m = new ModelWithOnlyStringFields()
{
Id = "998",
AlbumId = "112",
AlbumName = "ElectroShip",
Name = "QueryByExample"
};
db.Save<ModelWithOnlyStringFields>(m);
var modelFromDb = db.Where<ModelWithOnlyStringFields>(new {Name = "QueryByExample"})[0];
Assert.AreEqual(m.Name, modelFromDb.Name);
}
OrmLite.OrmLiteConfig.DialectProvider.NamingStrategy = new OrmLiteNamingStrategyBase();
}
开发者ID:ciker,项目名称:simplestack.ormlite,代码行数:27,代码来源:OrmLiteCreateTableWithNamigStrategyTests.cs
示例9: Can_Perform_CRUD_Operations_On_Table_With_Schema
public void Can_Perform_CRUD_Operations_On_Table_With_Schema()
{
var dbFactory = new OrmLiteConnectionFactory(
@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\App_Data\Database1.mdf;Integrated Security=True;User Instance=True",
SqlServerOrmLiteDialectProvider.Instance);
using (IDbConnection db = dbFactory.OpenDbConnection())
using (IDbCommand dbCmd = db.CreateCommand())
{
CreateSchemaIfNotExists(dbCmd);
dbCmd.CreateTable<User>(true);
dbCmd.Insert(new User { Id = 1, Name = "A", CreatedDate = DateTime.Now });
dbCmd.Insert(new User { Id = 2, Name = "B", CreatedDate = DateTime.Now });
dbCmd.Insert(new User { Id = 3, Name = "B", CreatedDate = DateTime.Now });
var lastInsertId = dbCmd.GetLastInsertId();
Assert.That(lastInsertId, Is.GreaterThan(0));
var rowsB = dbCmd.Select<User>("Name = {0}", "B");
Assert.That(rowsB, Has.Count.EqualTo(2));
var rowIds = rowsB.ConvertAll(x => x.Id);
Assert.That(rowIds, Is.EquivalentTo(new List<long> { 2, 3 }));
rowsB.ForEach(x => dbCmd.Delete(x));
rowsB = dbCmd.Select<User>("Name = {0}", "B");
Assert.That(rowsB, Has.Count.EqualTo(0));
var rowsLeft = dbCmd.Select<User>();
Assert.That(rowsLeft, Has.Count.EqualTo(1));
Assert.That(rowsLeft[0].Name, Is.EqualTo("A"));
}
}
开发者ID:Toshik,项目名称:ServiceStack.OrmLite,代码行数:35,代码来源:SchemaUseCase.cs
示例10: CanCreateSimpleForeignKey
public void CanCreateSimpleForeignKey()
{
using (var db = new OrmLiteConnectionFactory(ConnectionString, FirebirdDialect.Provider).Open())
{
db.CreateTable<TypeWithSimpleForeignKey>(true);
}
}
开发者ID:sami1971,项目名称:NServiceKit.OrmLite,代码行数:7,代码来源:ForeignKeyAttributeTests.cs
示例11: Setup
public void Setup()
{
using (var db = new OrmLiteConnectionFactory(ConnectionString, FirebirdDialect.Provider).Open())
{
db.CreateTable<ReferencedType>(true);
}
}
开发者ID:sami1971,项目名称:NServiceKit.OrmLite,代码行数:7,代码来源:ForeignKeyAttributeTests.cs
示例12: Can_Perform_CRUD_Operations_On_Table_With_Schema
public void Can_Perform_CRUD_Operations_On_Table_With_Schema()
{
var dbFactory = new OrmLiteConnectionFactory(
"User=SYSDBA;Password=masterkey;Database=ormlite-tests.fdb;DataSource=localhost;Dialect=3;charset=ISO8859_1;",
FirebirdOrmLiteDialectProvider.Instance);
using (IDbConnection db = dbFactory.OpenDbConnection())
{
db.CreateTable<User>(true);
db.Insert(new User { Id = 1, Name = "A", CreatedDate = DateTime.Now });
db.Insert(new User { Id = 2, Name = "B", CreatedDate = DateTime.Now });
db.Insert(new User { Id = 3, Name = "B", CreatedDate = DateTime.Now });
var lastInsertId = db.GetLastInsertId();
Assert.That(lastInsertId, Is.GreaterThan(0));
var rowsB = db.Select<User>("Name = {0}", "B");
Assert.That(rowsB, Has.Count.EqualTo(2));
var rowIds = rowsB.ConvertAll(x => x.Id);
Assert.That(rowIds, Is.EquivalentTo(new List<long> { 2, 3 }));
rowsB.ForEach(x => db.Delete(x));
rowsB = db.Select<User>("Name = {0}", "B");
Assert.That(rowsB, Has.Count.EqualTo(0));
var rowsLeft = db.Select<User>();
Assert.That(rowsLeft, Has.Count.EqualTo(1));
Assert.That(rowsLeft[0].Name, Is.EqualTo("A"));
}
}
开发者ID:yeurch,项目名称:ServiceStack.OrmLite,代码行数:34,代码来源:SchemaUseCase.cs
示例13: Can_select_from_FileStream
public void Can_select_from_FileStream()
{
ConnectionString = "Data Source=localhost;Initial Catalog=test2;User Id=test;Password=test;Connect Timeout=120;MultipleActiveResultSets=True;Type System Version=SQL Server 2012;";
var dialectProvider = SqlServerConverters.Configure(SqlServer2012Dialect.Provider);
var dbFactory = new OrmLiteConnectionFactory(ConnectionString, dialectProvider);
using (var db = dbFactory.OpenDbConnection())
{
db.DropTable<TestFile>();
db.CreateTable<TestFile>();
db.Insert(new TestFile { Contents = "contents".ToUtf8Bytes() });
db.Select<TestFile>().PrintDump();
//db.DropTable<FileStream>();
//db.CreateTable<FileStream>();
//db.Insert(new FileStream
//{
// Name = "file.txt",
// Path = SqlHierarchyId.Parse("/1/2/3/"),
// ParentPath = SqlHierarchyId.Parse("/1/2/"),
// FileContent = "contents".ToUtf8Bytes(),
// FileType = MimeTypes.PlainText,
//});
//var q = db.From<FileStream>();
//db.Select(q);
}
}
开发者ID:ServiceStack,项目名称:ServiceStack.OrmLite,代码行数:32,代码来源:FileStreamTests.cs
示例14: datetime_tests__can_use_datetime2
public void datetime_tests__can_use_datetime2()
{
var dbFactory = new OrmLiteConnectionFactory(base.ConnectionString, SqlServerOrmLiteDialectProvider.Instance);
//change to datetime2 - check for higher range and precision
SqlServerOrmLiteDialectProvider.Instance.UseDatetime2(true);
using(var conn = dbFactory.OpenDbConnection()) {
var test_object_ValidForDatetime2 = Table_for_datetime2_tests.get_test_object_ValidForDatetime2();
var test_object_ValidForNormalDatetime = Table_for_datetime2_tests.get_test_object_ValidForNormalDatetime();
conn.CreateTable<Table_for_datetime2_tests>(true);
//normal insert
var insertedId = conn.Insert(test_object_ValidForDatetime2, selectIdentity:true);
//read back, and verify precision
var fromDb = conn.SingleById<Table_for_datetime2_tests>(insertedId);
Assert.AreEqual(test_object_ValidForDatetime2.ToVerifyPrecision, fromDb.ToVerifyPrecision);
//update
fromDb.ToVerifyPrecision = test_object_ValidForDatetime2.ToVerifyPrecision.Value.AddYears(1);
conn.Update(fromDb);
var fromDb2 = conn.SingleById<Table_for_datetime2_tests>(insertedId);
Assert.AreEqual(test_object_ValidForDatetime2.ToVerifyPrecision.Value.AddYears(1), fromDb2.ToVerifyPrecision);
//check InsertParam
conn.Insert(test_object_ValidForDatetime2);
}
}
开发者ID:JackFong,项目名称:ServiceStack.OrmLite,代码行数:31,代码来源:Datetime2Tests.cs
示例15: Can_create_ModelWithFieldsOfDifferentTypes_table
public void Can_create_ModelWithFieldsOfDifferentTypes_table()
{
using (var db = new OrmLiteConnectionFactory(ConnectionString, FirebirdDialect.Provider).Open())
{
db.CreateTable<ModelWithFieldsOfDifferentTypes>(true);
}
}
开发者ID:sami1971,项目名称:NServiceKit.OrmLite,代码行数:7,代码来源:OrmLiteCreateTableTests.cs
示例16: Can_create_ModelWithIdOnly_table
public void Can_create_ModelWithIdOnly_table()
{
using (var db = new OrmLiteConnectionFactory(ConnectionString, FirebirdDialect.Provider).Open())
{
db.CreateTable<ModelWithIdOnly>(true);
}
}
开发者ID:sami1971,项目名称:NServiceKit.OrmLite,代码行数:7,代码来源:OrmLiteCreateTableTests.cs
示例17: Transaction_rollsback_transactions_to_different_tables
public void Transaction_rollsback_transactions_to_different_tables()
{
using (var db = new OrmLiteConnectionFactory(ConnectionString, FirebirdDialect.Provider).Open())
{
db.CreateTable<ModelWithIdAndName>(true);
db.CreateTable<ModelWithFieldsOfDifferentTypes>(true);
db.CreateTable<ModelWithOnlyStringFields>(true);
db.DeleteAll<ModelWithIdAndName>();
db.Insert(new ModelWithIdAndName(0));
using (var dbTrans = db.BeginTransaction())
{
db.Insert(new ModelWithIdAndName(0));
db.Insert(ModelWithFieldsOfDifferentTypes.Create(3));
db.Insert(ModelWithOnlyStringFields.Create("id3"));
Assert.That(db.Select<ModelWithIdAndName>(), Has.Count.EqualTo(2));
Assert.That(db.Select<ModelWithFieldsOfDifferentTypes>(), Has.Count.EqualTo(1));
Assert.That(db.Select<ModelWithOnlyStringFields>(), Has.Count.EqualTo(1));
}
Assert.That(db.Select<ModelWithIdAndName>(), Has.Count.EqualTo(1));
Assert.That(db.Select<ModelWithFieldsOfDifferentTypes>(), Has.Count.EqualTo(0));
Assert.That(db.Select<ModelWithOnlyStringFields>(), Has.Count.EqualTo(0));
}
}
开发者ID:ciker,项目名称:simplestack.ormlite,代码行数:26,代码来源:OrmLiteTransactionTests.cs
示例18: CanCreateForeignWithOnDeleteRestrict
public void CanCreateForeignWithOnDeleteRestrict()
{
using (var db = new OrmLiteConnectionFactory(ConnectionString, FirebirdDialect.Provider).Open())
{
db.CreateTable<TypeWithOnDeleteRestrict>(true);
}
}
开发者ID:sami1971,项目名称:NServiceKit.OrmLite,代码行数:7,代码来源:ForeignKeyAttributeTests.cs
示例19: Can_create_TableWithNamigStrategy_table_nameUnderscoreCoumpound
public void Can_create_TableWithNamigStrategy_table_nameUnderscoreCoumpound()
{
using (new TemporaryNamingStrategy(new UnderscoreSeparatedCompoundNamingStrategy()))
using (var db = new OrmLiteConnectionFactory(ConnectionString, FirebirdDialect.Provider).Open())
{
db.CreateTable<ModelWithOnlyStringFields>(true);
}
}
开发者ID:ServiceStack,项目名称:ServiceStack.OrmLite,代码行数:8,代码来源:OrmLiteCreateTableWithNamigStrategyTests.cs
示例20: TestFixtureSetUp
public void TestFixtureSetUp()
{
LogManager.LogFactory = new ConsoleLogFactory();
OrmLiteConfig.DialectProvider = OracleDialect.Provider;
OrmLiteConfig.ClearCache();
ConnectionString = GetFileConnectionString();
DbFactory = new OrmLiteConnectionFactory(ConnectionString, OracleDialect.Provider);
}
开发者ID:wrx362114,项目名称:ServiceStack.OrmLite,代码行数:9,代码来源:OrmLiteTestBase.cs
注:本文中的OrmLiteConnectionFactory类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论