Windows Phone 7.1支持SQL Server CE和LINQ to SQL,以及通过DatabaseSchemaUpdater升级数据库。
在其他平台上,我将读取数据库架构表(例如sys.objects
)以查看当前架构,并确定需要升级的表/列。
鉴于Windows Phone上不允许直接SQL访问,如何检索当前数据库架构?
最佳答案
SQL Server CE仍然包括INFORMATION_SCHEMA.TABLES
和INFORMATION_SCHEMA.COLUMNS
表,但是访问它们有点棘手,因为不允许直接SQL访问。
但是,您可以创建一个映射到以下表的DataContext
:
public class SchemaContext : DataContext
{
public SchemaContext()
: base("Data Source=isostore:/Database.sdf")
{
if (!this.DatabaseExists())
{
throw new InvalidOperationException("Cannot use the SchemaContext on a database which doesn't exist");
}
}
public Table<Table> Tables;
public Table<Column> Columns;
[Table(Name = "INFORMATION_SCHEMA.Columns")]
public class Column
{
[Column(Name = "TABLE_NAME")]
public string TableName { get; set; }
[Column(Name = "COLUMN_NAME")]
public string Name { get; set; }
[Column(Name = "DATA_TYPE")]
public string DataType { get; set; }
[Column(Name = "ORDINAL_POSITION")]
public int OrdinalPosition { get; set; }
[Column(Name = "IS_NULLABLE")]
public string IsNullableString { get; set; }
public bool IsNullable
{
get { return this.IsNullableString == "YES"; }
set { this.IsNullableString = value ? "YES" : "NO"; }
}
}
[Table(Name = "INFORMATION_SCHEMA.Tables")]
public class Table
{
[Column(Name = "TABLE_NAME")]
public string Name { get; set; }
[Column(Name = "TABLE_TYPE")]
public string Type { get; set; }
}
}
然后,您可以使用以下代码读取架构:
using (var schemaContext = new SchemaContext())
{
foreach (var table in schemaContext.Tables)
{
}
}
为这些表创建单独的上下文很重要,否则
DataContext.CreateDatabase
调用将尝试创建这些架构表,这将失败。
关于linq - Windows Phone SQL Server CE-检索基础数据库架构以进行升级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10996895/