1.使用Connection属性
dim dc as new pubsDatacontext
console.writeLine(dc.connection.connectionstring)
2.使用Transaction属性
添加System.Transactions引用。
Imports System.Data.SqlClient
Imports System.Data.Linq
Imports System.Linq
Imports System.Transactions '需要先添加引用
Module Module1
Private Function GetConnection() As SqlConnection
Return New SqlConnection(My.Settings.pubsConnectionString)
End Function
Private Sub testdbml()
Dim dc As New pubsDataContext
'Dim query As Table(Of titles) = dc.titles
Dim query = dc.titles
For Each item In query
Console.WriteLine("{0}:{1}", item.title_id, item.title)
Next
Console.WriteLine("Press Enter to exit")
End Sub
Private Sub testTransctions()
Dim dc As New pubsDataContext
Using theScope As New TransactionScope
Dim title1 As New titles
With title1
.title_id = "777779"
.title = "Professional XML"
.type = "programming"
.pubdate = "April 15,2007"
End With
dc.titles.InsertOnSubmit(title1)
Dim title2 As New titles
With title2
.title_id = "502242"
.title = "Professional VB 2010"
.type = "programming"
.pubdate = "June 15,2010"
End With
dc.titles.InsertOnSubmit(title2)
Try
Console.WriteLine("Before insert:{0} titles", dc.titles.Count)
dc.SubmitChanges()
Console.WriteLine("After insert:{0}", dc.titles.Count)
Catch ex As Exception
Console.WriteLine("ERROR:{0}", ex.Message)
End Try
theScope.Complete()
End Using
Console.WriteLine("Press enter to exit")
End Sub
Sub Main()
'testdbml()
testTransctions()
Console.Read()
End Sub
End Module
3.DataContext对象的其他方法和属性
方法
CreateDatabase()在服务器上创建数据库。
DatabaseExists()确定是否可以打开关联数据库。
DeleteDatabase()删除关联数据库。