日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
LINQtoSQL使用DataContext連接字符串

LINQ to SQL有很多值得學習的地方,這里我們主要介紹LINQ to SQL使用DataContext連接字符串,包括介紹創(chuàng)建和刪除數(shù)據(jù)庫等方面。

DataContext作為LINQ to SQL框架的主入口點,為我們提供了一些方法和屬性,本文用幾個例子說明DataContext幾個典型的應用。
◆CreateDatabase方法用于在服務器上創(chuàng)建數(shù)據(jù)庫。
◆DeleteDatabase方法用于刪除由DataContext連接字符串標識的數(shù)據(jù)庫。

數(shù)據(jù)庫的名稱有以下方法來定義:
◆如果數(shù)據(jù)庫在連接字符串中標識,則使用該連接字符串的名稱。
◆如果存在DatabaseAttribute屬性(Attribute),則將其Name屬性(Property)用作數(shù)據(jù)庫的名稱。
◆如果連接字符串中沒有數(shù)據(jù)庫標記,并且使用強類型的DataContext,則會檢查與DataContext繼承類名稱相同的數(shù)據(jù)庫。如果使用弱類型的DataContext,則會引發(fā)異常。
◆如果已通過使用文件名創(chuàng)建了DataContext,則會創(chuàng)建與該文件名相對應的數(shù)據(jù)庫。

我們首先用實體類描述關(guān)系數(shù)據(jù)庫表和列的結(jié)構(gòu)的屬性。再調(diào)用DataContext的CreateDatabase方法,LINQ to SQL會用我們的定義的實體類結(jié)構(gòu)來構(gòu)造一個新的數(shù)據(jù)庫實例。還可以通過使用 .mdf 文件或只使用目錄名(取決于連接字符串),將 CreateDatabase與SQL Server一起使用。LINQ to SQL使用DataContext連接字符串來定義要創(chuàng)建的數(shù)據(jù)庫和作為數(shù)據(jù)庫創(chuàng)建位置的服務器。

說了這么多,用一段實例說明一下吧!

首先,我們新建一個NewCreateDB類用于創(chuàng)建一個名為NewCreateDB.mdf的新數(shù)據(jù)庫,該數(shù)據(jù)庫有一個Person表,有三個字段,分別為PersonID、PersonName、Age。

 
 
 
  1. public class NewCreateDB : DataContext
  2. {
  3. public Table Persons;
  4. public NewCreateDB(string connection)
  5. :
  6. base(connection)
  7. {
  8. }
  9. public NewCreateDB(System.Data.IDbConnection connection)
  10. :
  11. base(connection)
  12. {
  13. }
  14. }
  15. [Table(Name = "Person")]
  16. public partial class Person : INotifyPropertyChanged
  17. {
  18. private int _PersonID;
  19. private string _PersonName;
  20. private System.Nullable _Age;
  21. public Person() { }
  22. [Column(Storage = "_PersonID", DbType = "INT",
  23. IsPrimaryKey = true)]
  24. public int PersonID
  25. {
  26. get { return this._PersonID; }
  27. set
  28. {
  29. if ((this._PersonID != value))
  30. {
  31. this.OnPropertyChanged("PersonID");
  32. this._PersonID = value;
  33. this.OnPropertyChanged("PersonID");
  34. }
  35. }
  36. }
  37. [Column(Storage = "_PersonName", DbType = "NVarChar(30)")]
  38. public string PersonName
  39. {
  40. get { return this._PersonName; }
  41. set
  42. {
  43. if ((this._PersonName != value))
  44. {
  45. this.OnPropertyChanged("PersonName");
  46. this._PersonName = value;
  47. this.OnPropertyChanged("PersonName");
  48. }
  49. }
  50. }
  51. [Column(Storage = "_Age", DbType = "INT")]
  52. public System.Nullable Age
  53. {
  54. get { return this._Age; }
  55. set
  56. {
  57. if ((this._Age != value))
  58. {
  59. this.OnPropertyChanged("Age");
  60. this._Age = value;
  61. this.OnPropertyChanged("Age");
  62. }
  63. }
  64. }
  65. public event PropertyChangedEventHandler PropertyChanged;
  66. protected virtual void OnPropertyChanged(string PropertyName)
  67. {
  68. if ((this.PropertyChanged != null))
  69. {
  70. this.PropertyChanged(this,
  71. new PropertyChangedEventArgs(PropertyName));
  72. }
  73. }
  74. }

接下來的一段代碼先創(chuàng)建一個數(shù)據(jù)庫,在調(diào)用CreateDatabase后,新的數(shù)據(jù)庫就會存在并且會接受一般的查詢和命令。接著插入一條記錄并且查詢。***刪除這個數(shù)據(jù)庫。

 
 
 
  1. //新建一個臨時文件夾來存放新建的數(shù)據(jù)庫
  2. string userTempFolder = Environment.GetEnvironmentVariable
  3. ("SystemDrive") + @"\YJingLee";
  4.  Directory.CreateDirectory(userTempFolder);
  5. //新建數(shù)據(jù)庫NewCreateDB
  6. string userMDF = System.IO.Path.Combine(userTempFolder,
  7. @"NewCreateDB.mdf");
  8. string connStr = String.Format(@"Data Source=.\SQLEXPRESS;
  9. AttachDbFilename={0};Integrated Security=True;
  10. Connect Timeout=30;User Instance=True; 
  11. Integrated Security = SSPI;", userMDF);
  12. NewCreateDB newnewDB = new NewCreateDB(connStr);
  13. newDB.CreateDatabase();
  14. //插入數(shù)據(jù)并查詢
  15. var newnewRow = new Person
  16. {
  17. PersonID = 1,
  18. PersonName = "YJingLee",
  19. Age = 22
  20. };

本文標題:LINQtoSQL使用DataContext連接字符串
標題URL:http://www.5511xx.com/article/cospoic.html