C# 如何使用WindowsPhone8中的属性在SQLite中创建多个表
如何使用实体类创建多个表C# 如何使用WindowsPhone8中的属性在SQLite中创建多个表,c#,sqlite,windows-phone-8,C#,Sqlite,Windows Phone 8,如何使用实体类创建多个表 public class Info { public string firstName { get; set; } public string lastName { get; set; } public string imageUrl { get; set; } [PrimaryKey, AutoIncrement] public int Id { get; set; }
public class Info
{
public string firstName { get; set; }
public string lastName { get; set; }
public string imageUrl { get; set; }
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public DetailInfo objDetailInfo{ get; set; }
}
public class DetailInfo
{
public string phoneno { get; set; }
public string address { get; set; }
}
我正在使用下面的代码
public void createtable()
{
SQLite.SQLiteConnection db= new SQLite.SQLiteConnection(dbPath);
db.CreateTable<Info>();
var data = new Info() { Id = "1", firstName = "Rehan", imageUrl = "safsdfsdf", lastName = "Parvez", objsty = new objDetailInfo{ address = "Nagpur", phoneno = "902136" } };
db.Insert(data);
}
public void createtable()
{
SQLite.SQLiteConnection db=new SQLite.SQLiteConnection(dbPath);
db.CreateTable();
var data=new Info(){Id=“1”,firstName=“Rehan”,imageUrl=“safsdfsdf”,lastName=“Parvez”,objsty=new objDetailInfo{address=“Nagpur”,phoneno=“902136”};
插入(数据);
}
执行
db.CreateTable()
时,它给了我一个错误我自己回答我的问题,因为我认为它会帮助我的其他朋友
使用忽略关键字
public class Info
{
public string firstName { get; set; }
public string lastName { get; set; }
public string imageUrl { get; set; }
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
[Ignore]
public DetailInfo objDetailInfo{ get; set; }
}
public class DetailInfo
{
public string phoneno { get; set; }
public string address { get; set; }
}
public void createtable()
{
SQLite.SQLiteConnection db= new SQLite.SQLiteConnection(dbPath);
db.CreateTable<Info>();
db.CreateTable<DetailInfo>();
}
公共类信息
{
公共字符串名{get;set;}
公共字符串lastName{get;set;}
公共字符串imageUrl{get;set;}
[主密钥,自动增量]
公共int Id{get;set;}
[忽略]
public DetailInfo objDetailInfo{get;set;}
}
公共类详细信息
{
公共字符串phoneno{get;set;}
公共字符串地址{get;set;}
}
公共void createtable()
{
SQLite.SQLiteConnection db=new SQLite.SQLiteConnection(dbPath);
db.CreateTable();
db.CreateTable();
}
是另一种方式,如sqlite net所述
萨默里
public class Stock
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
[MaxLength(8)]
public string Symbol { get; set; }
}
public class Valuation
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
[Indexed]
public int StockId { get; set; }
public DateTime Time { get; set; }
public decimal Price { get; set; }
}
有了这些,您可以通过调用CreateTable在数据库中自动生成表:
var db = new SQLiteConnection("stocks.db");
db.CreateTable<Stock>();
db.CreateTable<Valuation>();
您可以使用SQLiteConnection的查询方法查询数据库:
public static IEnumerable<Valuation> QueryValuations (SQLiteConnection db, Stock stock)
{
return db.Query<Valuation> ("select * from Valuation where StockId = ?", stock.Id);
}
公共静态IEnumerable查询值(SQLiteConnection db,Stock)
{
返回db.Query(“从估价中选择*,其中StockId=?”,stock.Id);
}
还可以使用Linq语法编写此查询,以便键入safety并确保重构是安全的:
public static IEnumerable<Valuation> QueryValuations (SQLiteConnection db, Stock stock)
{
return db.Table<Valuation> ().Where(v => v.StockId == stock.Id);
}
公共静态IEnumerable查询值(SQLiteConnection db,Stock)
{
返回db.Table(),其中(v=>v.StockId==stock.Id);
}
查询方法的泛型参数指定要为每行创建的对象类型。它可以是一个表类,也可以是公共属性与查询返回的列匹配的任何其他类。Table方法要求存在与返回的类型同名的表
public class Val {
public decimal Money { get; set; }
public DateTime Date { get; set; }
}
public static IEnumerable<Val> QueryVals (SQLiteConnection db, Stock stock)
{
return db.Query<Val> ("select \"Price\" as \"Money\", \"Time\" as \"Date\" from Valuation where StockId = ?", stock.Id);
}
公共类Val{
公共十进制货币{get;set;}
公共日期时间日期{get;set;}
}
公共静态IEnumerable查询文件(SQLiteConnection数据库,库存)
{
返回db.Query(“从StockId=?”、stock.Id的估值中选择“价格”作为“货币”,选择“时间”作为“日期”);
}
mscorlib.ni.dll中发生了“System.NotSupportedException”类型的异常,但未在用户代码中处理。如果存在此异常的处理程序,则程序可以安全地继续运行。有人知道此问题的答案吗?我们需要解决办法。帮助我们。
public class Val {
public decimal Money { get; set; }
public DateTime Date { get; set; }
}
public static IEnumerable<Val> QueryVals (SQLiteConnection db, Stock stock)
{
return db.Query<Val> ("select \"Price\" as \"Money\", \"Time\" as \"Date\" from Valuation where StockId = ?", stock.Id);
}