16,549
社区成员
发帖
与我相关
我的任务
分享
Imports System.Data.Common
Public Class Form1
Private db As New LzmTW.Data.SqlDatabase("(local)", "NorthWind")
Private cm As DbCommand
Private ds As New DataSet
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
cm = db.CreateCommand("[Sales by Year]", True)
db.AddParameter(cm, "Beginning_Date").DbType = DbType.DateTime
db.AddParameter(cm, "Ending_Date").DbType = DbType.DateTime
db.SetParameterValue(cm, "Beginning_Date", #7/4/1996#)
db.SetParameterValue(cm, "Ending_Date", #9/4/1996#)
db.Load(cm, ds, "1996")
db.SetParameterValue(cm, "Beginning_Date", #7/4/1997#)
db.SetParameterValue(cm, "Ending_Date", #9/4/1997#)
db.Load(cm, ds, "1997")
db.Load("SELECT * FROM ORDERS;SELECT * FROM REGION", ds, New String() {"ORDERS", "REGION"})
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.DataGridView1.DataSource = ds.Tables("1996")
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Me.DataGridView1.DataSource = ds.Tables("1997")
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
Me.DataGridView1.DataSource = ds.Tables("ORDERS")
End Sub
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
Me.DataGridView1.DataSource = ds.Tables("REGION")
End Sub
Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
Dim db As New LzmTW.Data.ExcelDatabase("g:\dd.xls")
Console.WriteLine(db.ToExcel(New String() {"[AA$]", "[BB$]"}, "g:\t.xls", New String() {"AA", "BB"}))
End Sub
End Class
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim connectionString1 As String = "Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;"
Dim connectionString2 As String = "Data Source= myServerAddress ;Initial Catalog= myDataBase ;User Id= myUsername ;Password=' myPassword ';"
Dim b1 As New SqlClient.SqlConnectionStringBuilder(connectionString1)
Dim b2 As New SqlClient.SqlConnectionStringBuilder(connectionString2)
Console.WriteLine("equals Data Source? {0} ,value is {1}", b1.DataSource.Equals(b2.DataSource), b2.DataSource)
Console.WriteLine("equals Inital Catalog? {0} ,value is {1}", b1.InitialCatalog.Equals(b2.InitialCatalog), b2.InitialCatalog)
Console.WriteLine("equals User ID? {0} ,value is {1}", b1.UserID.Equals(b2.UserID), b2.UserID)
Console.WriteLine("equals Password? {0} ,value is {1}", b1.Password.Equals(b2.Password), b2.Password)
End Sub
'结果:
'equals Data Source? True ,value is myServerAddress
'equals Inital Catalog? True ,value is myDataBase
'equals User ID? True ,value is myUsername
'equals Password? False ,value is myPassword
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim connectionString1 As String = "Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;"
Dim connectionString2 As String = "Data Source= myServerAddress ;Initial Catalog= myDataBase ;User Id= myUsername ;Password= myPassword ;"
Dim b1 As New SqlClient.SqlConnectionStringBuilder(connectionString1)
Dim b2 As New SqlClient.SqlConnectionStringBuilder(connectionString2)
Console.WriteLine("equals Data Source? {0} ,value is {1}", b1.DataSource.Equals(b2.DataSource), b1.DataSource)
Console.WriteLine("equals Inital Catalog? {0} ,value is {1}", b1.InitialCatalog.Equals(b2.InitialCatalog), b1.InitialCatalog)
Console.WriteLine("equals User ID? {0} ,value is {1}", b1.UserID.Equals(b2.UserID), b1.UserID)
Console.WriteLine("equals Password? {0} ,value is {1}", b1.Password.Equals(b2.Password), b1.Password)
End Sub
'结果:
'equals Data Source? True ,value is myServerAddress
'equals Inital Catalog? True ,value is myDataBase
'equals User ID? True ,value is myUsername
'equals Password? True ,value is myPassword
Option Strict Off
Imports System.Runtime.InteropServices
Public Class Form1
Private Function CreateOfficeApplication(ByVal progID As String) As Object
Return CreateObject(progID)
End Function
Dim t1 As New LzmTW.MSOffice.ComBuilder
Dim t2 As New LzmTW.MSOffice.ComBuilder
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
t1.Create(AddressOf CreateOfficeApplication, "Excel.Application", "EXCEL")
t1.ComApplication.Visible = True
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
t2.Create(AddressOf CreateOfficeApplication, "Excel.Application", "EXCEL")
t2.ComApplication.Visible = True
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
t1.ComApplication.Quit()
t1.Release()
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
t2.ComApplication.Quit()
t2.Release()
End Sub
End Class
Imports System.Runtime.InteropServices
Namespace LzmTW.MSOffice
Public Delegate Function CreateComHanlder(ByVal progID As String) As Object
Public Class ComBuilder
Private gComProcess As Process
Private gComApplication As Object
Private gValid As Boolean = False
Public Property Valid() As Boolean
Get
Return gValid
End Get
Private Set(ByVal value As Boolean)
gValid = value
End Set
End Property
Public Property ComProcess() As Process
Get
Return gComProcess
End Get
Private Set(ByVal value As Process)
gComProcess = value
End Set
End Property
Public Property ComApplication() As Object
Get
Return gComApplication
End Get
Private Set(ByVal value As Object)
gComApplication = value
End Set
End Property
Public Sub Create(ByVal createCom As CreateComHanlder, ByVal progID As String, ByVal processName As String)
If Me.Valid Then
Throw New Exception("Com 已存在")
End If
InternalBuildInstance(createCom, progID, processName)
End Sub
Private Sub InternalBuildInstance(ByVal d As CreateComHanlder, ByVal progID As String, ByVal processName As String)
Dim beforeTime As Date
Dim afterTime As Date
Dim processTime As Date
beforeTime = Now
Me.ComApplication = d.Invoke(progID)
afterTime = Now
If Me.ComApplication Is Nothing Then
Throw New Exception("无法获取 Com 实例")
End If
If Not Marshal.IsComObject(Me.ComApplication) Then
Me.ComApplication = Nothing
Throw New Exception("非Com")
End If
Dim mProcessList As Process()
mProcessList = Process.GetProcessesByName(processName)
If mProcessList.Length = 0 Then
Me.ReleaseCom()
Throw New Exception("processName 无效")
End If
For Each p As Process In mProcessList
processTime = p.StartTime
If processTime.CompareTo(beforeTime) > 0 AndAlso processTime.CompareTo(afterTime) < 0 Then
Me.ComProcess = p
Exit For
End If
Next
If Me.ComProcess Is Nothing Then
Me.ReleaseCom()
Throw New Exception("无法定位process")
End If
Me.Valid = True
End Sub
Public Sub Release()
If Not Me.Valid Then Return
Me.ReleaseCom()
Me.Kill()
Me.Valid = False
End Sub
Private Sub ReleaseCom()
Marshal.ReleaseComObject(Me.ComApplication)
Me.ComApplication = Nothing
GC.Collect(2)
End Sub
Private Sub Kill()
Me.ComProcess.Kill()
Me.ComProcess.Dispose()
Me.ComProcess = Nothing
End Sub
End Class
End Namespace