2011年2月28日 星期一

SqlBulkCopy.WriteToServer 方法 使用DataRow 陣列大量載入資料

這範例是微軟官網的,寫得技巧還不錯;值得收藏!!
SqlBulkCopy.WriteToServer 方法 (DataRow[])

-------
Imports System.Data.SqlClient

Module Module1
Sub Main()
Dim connectionString As String = GetConnectionString()

' Open a connection to the AdventureWorks database.
Using connection As SqlConnection = _
New SqlConnection(connectionString)
connection.Open()

' Perform an initial count on the destination table.
Dim commandRowCount As New SqlCommand( _
"SELECT COUNT(*) FROM dbo.BulkCopyDemoMatchingColumns;", _
connection)
Dim countStart As Long = _
System.Convert.ToInt32(commandRowCount.ExecuteScalar())
Console.WriteLine("Starting row count = {0}", countStart)

' Create a table with some rows.
Dim newProducts As DataTable = MakeTable()

' Get a reference to a single row in the table.
Dim rowArray() As DataRow = newProducts.Select( _
"Name='CC-101-BK'")

' Note that the column positions in the source DataTable
' match the column positions in the destination table,
' so there is no need to map columns.
Using bulkCopy As SqlBulkCopy = _
New SqlBulkCopy(connection)
bulkCopy.DestinationTableName = "dbo.BulkCopyDemoMatchingColumns"

Try
' Write the array of rows to the destination.
bulkCopy.WriteToServer(rowArray)

Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
End Using

' Perform a final count on the destination table
' to see how many rows were added.
Dim countEnd As Long = _
System.Convert.ToInt32(commandRowCount.ExecuteScalar())
Console.WriteLine("Ending row count = {0}", countEnd)
Console.WriteLine("{0} rows were added.", countEnd - countStart)

Console.WriteLine("Press Enter to finish.")
Console.ReadLine()
End Using
End Sub

Private Function MakeTable() As DataTable
' Create a new DataTable named NewProducts.
Dim newProducts As DataTable = _
New DataTable("NewProducts")

' Add three column objects to the table.
Dim productID As DataColumn = New DataColumn()
productID.DataType = System.Type.GetType("System.Int32")
productID.ColumnName = "ProductID"
productID.AutoIncrement = True
newProducts.Columns.Add(productID)

Dim productName As DataColumn = New DataColumn()
productName.DataType = System.Type.GetType("System.String")
productName.ColumnName = "Name"
newProducts.Columns.Add(productName)

Dim productNumber As DataColumn = New DataColumn()
productNumber.DataType = System.Type.GetType("System.String")
productNumber.ColumnName = "ProductNumber"
newProducts.Columns.Add(productNumber)

' Create an array for DataColumn objects.
Dim keys(0) As DataColumn
keys(0) = productID
newProducts.PrimaryKey = keys

' Add some new rows to the collection.
Dim row As DataRow
row = newProducts.NewRow()
row("Name") = "CC-101-WH"
row("ProductNumber") = "Cyclocomputer - White"
newProducts.Rows.Add(row)

row = newProducts.NewRow()
row("Name") = "CC-101-BK"
row("ProductNumber") = "Cyclocomputer - Black"
newProducts.Rows.Add(row)

row = newProducts.NewRow()
row("Name") = "CC-101-ST"
row("ProductNumber") = "Cyclocomputer - Stainless"
newProducts.Rows.Add(row)
newProducts.AcceptChanges()

' Return the new DataTable.
Return newProducts
End Function

Private Function GetConnectionString() As String
' To avoid storing the connection string in your code,
' you can retrieve it from a configuration file.
Return "Data Source=(local);" & _
"Integrated Security=true;" & _
"Initial Catalog=AdventureWorks;"
End Function
End Module

2011年2月27日 星期日

xp 修复 mdac 2.8

1.微軟
2.在windows xp sp2 下修复 mdac 2.8
-----------
重點:
c:\windows\inf\mdac.inf ,點右鍵執行安裝;放入光碟片或採第2參考方式(已包出來)

2011年2月23日 星期三

vbnet 使用方式 VarPtr()

參考
--------
Imports System.Runtime.InteropServices

Public Class Form1

Public Data(40) As Byte

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MessageBox.Show(VarPtr(Data(0)))
End Sub

Public Function VarPtr(ByVal obj As Object) As Integer
Dim GC As GCHandle = GCHandle.Alloc(obj, GCHandleType.Pinned)
Return GC.AddrOfPinnedObject.ToInt32
GC.Free()
End Function

End Class

cmd dos net view share

System.Diagnostics.Process.Start("CMD.exe", "/c net view > c:\PCList.txt")

2011年2月22日 星期二

windows cmd dos net 指令運用

一 net use作用

Net use將計算機與網絡其他計算機共享資源連接或斷開,或者顯示關於計算機共享連接的信息。

一般的我們從網絡路徑拷貝文件到本地,或從本地拷貝文件到一個網絡路徑,但是如果一個網絡路徑太長的話,有的時候會有問題,這個時候的解決辦法就是先把網絡路徑映射為本地的一個盤符,然後使用這個映射的盤符代替網絡路徑。

還有如果某些網絡其他的計算機的共享我們需要經常使用,我們就可以直接映射為我們本地的一個盤。這個情況類似與我們將本地的目錄映射為一個盤的subst。

net use如果在沒有參數的情況下使用,則 net use 檢索網絡連接列表。

二 net use語法
NET USE
[devicename | *] [\\computername\sharename[\volume] [password | *]]
[/USER:[domainname\]username]
[/USER:[dotted domain name\]username]
[/USER:[username@dotted domain name]
[/SMARTCARD]
[/SAVECRED]
[[/DELETE] | [/PERSISTENT:{YES | NO}]]

NET USE {devicename | *} [password | *] /HOME

NET USE [/PERSISTENT:{YES | NO}]

參數介紹:
DeviceName 指派名稱以便連接到資源或指定斷開的設備。有兩種類型的設備名: 磁盤驅動器(即 D: 到 Z:} 和打印機(即 LPT1:到 LPT3:}。鍵入星號而不是特定設備名,指派下一個可用的設備名。

\\\\ComputerName\\ShareName 指定服務器和共享資源的名稱。如果 ComputerName 包含有空格,請使用引號將從雙反斜線符號 (\\\\) 到計算機名尾的整個計算機命名引起來(例如,」\\\\Computer Name\\Share Name」)。計算機名長度可以是 1 到 15 個字符。

\\volume 指定服務器上的 NetWare 卷。必須安裝並運行 Client Service for NetWare(NetWare 客戶服務)才能連接 NetWare 服務器。

Password 指定訪問共享資源所需的密碼。輸入星號 (*) 產生一個密碼提示在密碼提示行處鍵入密碼時不顯示密碼。

/user 指定建立連接的不同用戶名。

DomainName 指定其他域。如果省略 DomainName, net use 將使用當前登錄的域。

UserName 指定登錄時使用的用戶名。

DottedDomainName 指定用戶帳戶所在域完全合格的域名。

/savecred 存儲為重新使用提供的憑據。

/smartcard 指定網絡連接將使用的智能卡憑據。如果有多個智能卡可用,則需要指定憑據。

/delete 取消指定的網絡連接。如果用戶使用星號 (*) 指定連接,則所有網絡連接均將取消。

/persistent:{yes | no} 控制持久網絡連接的使用。默認值為最後一次使用的設置。非設備連接不會持久。Yes 將按其建立時的原樣保存所有連接,並在下次登錄時還原它們。No 則不保存已建立的連接或後續連接。現存的連接在下一次登錄時還原。使用 /delete 刪除持久連接。

/home 將用戶連到主目錄。

net help command 顯示指定 net 命令的幫助。

三 net use使用實例
net use h: \\ip\c$ 「密碼」 /user:」用戶名」 直接登陸後映射對方C:到本地為H:
net use h: /del 刪除映射對方到本地的為H:的映射

net use z: \\遠程主機名\共享目錄 用戶名密碼 /user:管理員權限的用戶名(可以為域用戶) 映射遠程的共享目錄為Z
rem net user z: /del 刪除Z

[注意:如果路徑或用戶名密碼有空格,則使用雙引號括起來]

四 其他的net命令

(以管理員身份登錄遠程主機) net use \\IP地址\ipc$ 「密碼」 /use:」Administrator」

(傳送文件到遠程主機WINNT目錄下)copy 本機目錄路徑\程序 \\IP地址\admin$

(查看遠程主機時間) net time \\IP地址

(定時啟動某個程序) at \\IP地址 02:18 readme.exe

(查看共享) net view \\IP地址

(將遠程主機C盤映射為自己的F盤) net use f: \\IP地址\c$ 「」/user:」Administrator」

(斷開連接) net use \\IP地址\ipc$ /delete

2011年2月17日 星期四

sql 時間區間

declare @s varchar(8000),@speed int,@end int,@i int,@start int
set @speed=300
set @i=1
set @start=100
set @end=1000
set @s='select '
while @i<@end/@speed+1 begin set @s=@s+'['+cast(@start as varchar)+'-'+cast(@start+@speed as varchar)+']=sum(case when 使用时间 >='+cast(@start as varchar)+' and 使用时间 < '+cast(@start+@speed as varchar)+' then 1 else 0 end),' set @i=@i+1 set @start=@start+@speed end set @s=stuff(@s,len(@s),1,' from tb') print @s select [100-400]=sum(case when 使用时间 >=100 and 使用时间 < 400 then 1 else 0 end),[400-700]=sum(case when 使用时间 >=400 and 使用时间 < 700 then 1 else 0 end),[700-1000]=sum(case when 使用时间 >=700 and 使用时间 < 1000 then 1 else 0 end) from tb

SQL 数值区间分组查询

如何将某一字段按等差区间分组?
如字段girl为int型,当给定初值为5,终值为108,公差为20时 将字段Girl的值分为(0-4),(5-24),(25-44),(45-64),(65-84),(85-104),(104,108)。。。这样7组,并统计落在每段内的记录个数


group by (girl+15)/20


如果说要取以100为基准的那就是

group by(girl+100)/100


SELECT COUNT(1) AS cou, (sp_price + 500) / 500 AS spp
FROM systemic_prop
GROUP BY (sp_price + 500) / 500

2011年2月14日 星期一

DataRelation 相關參考

1.Know Dot Net - ADO.NET's DataRelation Object
2.DataRelation: Create a relation between the two tables : Table Relation « Database ADO.net « VB.Net
3.なおこ(・∀・)のニヤニヤする備忘録
-----
表達方式:
Dim parentColumns as DataColumn() = new DataColumn() { parentColumn1, parentColumn2 }

Dim childColumns as Datacolumn() = new Datacolumn() { childColumn1, childColumn2 }

Dim relation1 as DataRelation = new DataRelation("relationName", parentColumns, childColumns)

dataset.Relations.Add(relation1)

2011年2月9日 星期三

NPOI、OpenXML SDK、OpenOffice.org SDK 寫入資料到 EXCEL 檔案

請參考(很詳細)

Automate OpenOffice from VB.NET

參考網址
Automate OpenOffice from VB.NET
Though from user's point of view OO Calc is very similar to MS Excel, these two products have completelly different API object models. OO offers its own UNO API which is not compatible with MS Office object model at least directly.

There are several ways to program OpenOffice Calc from dot net languages like VB.NET or C#:

1) Use .net UNO bindings or late binding to UNO

2) Use compatibility layer libraries making OO Calc object model to look similar to MS Excel object model. There are several options:

2.1) Etersoft UnoOffice DLL implements a subset of MS Excel COM interface using OO Calc. So it translates calls to MS Excel object model to OO Calc UNO. This library has support even for OO v3 and many properties and methods are already implemented.

You can download an installation package (MSI file) at: http://updates.etersoft.ru/pub/Etersoft/UniOffice@Etersoft/0.4/Windows/

It installs unioffice_excel.dll and unioffice_excel.tlb to C:\WINDOWS\system32\ You can see a list of implemented methods in any COM object browser, for example in VBA by adding a reference to unioffice_excel.tlb. Unioffice_excel.dll contains only implementation code without any metadata. It is even possible to run many different applications expecting MS Excel installed and they will begin to work with OO Calc. Also you can use MS Excel primary interop assembly instead of unioffice_excel.tlb. In such case you may need to cast typed instances of objects like Sheet or Range to an IUnknown using CObj() function in VB.NET, I expect it is related with a lack of duality in some MS Excel interfaces.

A sample VB.NET code:

Dim EA As New Excel.Application
EA.Visible = True
Dim WB As Excel.Workbook = EA.Workbooks.Open("C:\Download\Test.xls")
Dim S As String = WB.ActiveSheet().Cells(1, 1).Value
Debug.Print(S)
EA.Quit()

Crystal Reports for Visual Studio 2010 beta

SAP BusinessObjects Crystal Reports for Visual Studio 2010
微軟的Visual Studio 2010正式版本, 已經取消對 Crystal Reports for Visual Studio 2010的支援,不過別擔心, SAP已經提供免費版本(Beta)下載

2011年2月7日 星期一

各簡訊中心網站

1.中華電信簡訊中心
2.Socket to Air COM (hinet)vb6

Split String 好的範例

參考來源
---
真是精妙,值得參考
--- Program that uses Split on String [VB.NET] ---

Module Module1

Sub Main()
' We want to split this input string
Dim s As String = "there is a cat"

' Split string based on spaces
Dim words As String() = s.Split(New Char() {" "c})

' Use For Each loop over words and display them
Dim word As String
For Each word In words
Console.WriteLine(word)
Next
End Sub

End Module