码迷,mamicode.com
首页 > 数据库 > 详细

Windows Phone本地数据库(SQLCE):9、Connection Strings(翻译) (转)

时间:2014-07-12 16:18:12      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   使用   strong   文件   

这是“windows phone mango本地数据库(sqlce)”系列短片文章的第八篇。 为了让你开始在Windows Phone Mango中使用数据库,这一系列短片文章将覆盖所有你需要知道的知识点。我将谈谈在windows phone mango本地数据库中使用Connection Strings的问题。

1、ConnectionStrings是什么

    在我们实际开始使用一个数据库之前,我们需要制定一个连接字符串,它告诉应用程序怎么连接数据库。一个连接字符串可以被用来做数据库的配置值。在连接字符串里,每个参数通过分号分开,参数值放在引号里。一些参数仅适用于创建数据库;在数据库创建之后,这些参数就被忽略了。
    一个特殊格式的连接字符串应该是这样的:
"Data Source=‘isostore:/DIRECTORY/FILE.sdf‘";
 

2、怎么使用ConnectionStrings

示例1:一个参数的用法
String format: "Data Source=‘isostore:/DIRECTORY/FILE.sdf‘";
注释isostore 表示指向IsolatedStorage的路径
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
private const string ConnectionString = @"isostore:/CountryDB.sdf";
 
public MainPage()
{
     InitializeComponent();
  
     using (CountryDataContext context = new CountryDataContext(ConnectionString))
     {
  
         if (!context.DatabaseExists())
         {
             // create database if it does not exist
             context.CreateDatabase();
         }
     }
}
 
示例2:从安装文件夹读取 String format: "Data Source=‘appdata:/DIRECTORY/FILE.sdf‘"; 注释:appdata表示指向安装文件夹的路径
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
private const string ConnectionString = @"Data Source = ‘appdata:/CountryDB.sdf‘; File Mode = read only;";
public MainPage()
{
     InitializeComponent();
  
     using (CountryDataContext context = new CountryDataContext(ConnectionString))
     {
  
         if (!context.DatabaseExists())
         {
             // create database if it does not exist
             context.CreateDatabase();
         }
     }
}

 

示例3:带有特定的Culture的数据库

1
private const string ConnectionString = @"Data Source = ‘CountryDB.sdf‘; Culture Identifier = fr-FR; Case Sensitive = true;";

注释:你可以参考MSDN文档:http://msdn.microsoft.com/zh-cn/library/system.globalization.cultureinfo(v=vs.71).aspx

示例4:数据库加密

String format: "Data Source=‘isostore:/DIRCTORY/FILE.sdf‘;Password=‘SomePassword‘"

 

1
private const string ConnectionString = @"Data Source=‘isostore:/CountryDB.sdf‘;Password=‘MyPassword‘;";

 

  这篇文章我谈论了在windows phone mango本地数据库中的连接字符串以及如何使用它。请继续关注接下来的文章。

Windows Phone本地数据库(SQLCE):9、Connection Strings(翻译) (转),布布扣,bubuko.com

Windows Phone本地数据库(SQLCE):9、Connection Strings(翻译) (转)

标签:style   blog   http   使用   strong   文件   

原文地址:http://www.cnblogs.com/zgqys1980/p/3838276.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!