2009年9月30日 星期三

ASP.NET - IBM DB2 工具列

原來~ 要在 Visual Studio 裡使用 DB2 Connection 來連結資料庫
一定要先裝好 DB2 後再安裝 Visual Studio
這樣 VS 就會自動將 DB2 工具列加進來了!

20091007補充:
其實只要先裝好資料庫再裝 VS
VS 就會自動將資料庫工具列加入!

2009年9月29日 星期二

ASP.NET - 上傳檔案到資料庫

在 APS.NET 1.1 裡要將檔案存到資料庫!!??

因為專案裡所使用的資料庫為 DB2 , 所以必需將儲存檔案的欄位設為 BLOB 以儲存 Binary 的資料

我這次要存的檔案為 pdf 檔, 所以先利用 CrystalReport 產生好 pdf 後,

再讀取 pdf 檔並轉成 Binary, 存入資料庫

Dim myFileStream As New System.IO.FileStream("abc.pdf", IO.FileMode.Open, IO.FileAccess.Read)
Dim myBinaryReader As New System.IO.BinaryReader(myFileStream)
Dim bteRead() As Byte = myBinaryReader.ReadBytes(myFileStream.Length)
Dim MyConnection As New System.Data.oledb.oledbConnection("連線字串")
Dim MyCommand As New System.Data.oledb.oledbCommand

MyConnection.Open()

MyCommand.Connection = MyConnection
MyCommand.CommandText = "insert into TableName(sFile) Values(?) "
MyCommand.Parameters.Add("sFile", oledb.oledbType.Binary).Value = bteRead
MyCommand.ExecuteReader()
MyConnection.Close()

----------------------------------------------------------

從資料庫下載檔案:

Dim MyConnection As New System.Data.oledb.oledbConnection("連線字串")
Dim MyCommand As New System.Data.oledb.oledbCommand

MyConnection.Open()
MyCommand.Connection = MyConnection
MyCommand.CommandText = "select sFile from TableName"

Response.ClearHeaders()
Response.Clear()
Response.Expires = 0
Response.Buffer = True

Response.AddHeader("content-disposition", "attachment; filename=abc.pdf")
Response.ContentType = "Application/octet-stream"
Response.BinaryWrite(MyCommand.ExecuteScalar)
MyConnection.Close()
'釋放資源
Response.End()

MyConnection.Open()

----------------------------------------------------------

那如果是在 ASP.NET 2.0 要上傳檔案至資料庫呢?

就可以使用 FileUpload 來達成, 可參考 如何在ASP.NET中上傳檔案到資料庫

2009年7月22日 星期三

SQL Server 2008 Express 無法遠端連線??

錯誤訊息:
provider: 具名的管線提供者, error: 40 - 無法開啟至 SQL Server 的連接

這個問題找了好久....
原來是 port 的問題
Express 不是預設的 1433 port , 我裝的機器裡是 1082 port
難怪怎麼連都連不到!!

查看 sql server 的 port :
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\SQLEXPRESS\MSSQLServer\SuperSocketNetLib\Tcp


20100710 補充
在 sql server configuration manager 裡要把 TPC/IP 設為啟用才行

2009年1月21日 星期三

ASP.NET - 在 DataGrid 排序

要在 asp.net 1.1 中的 DataGrid 裡做排序
可以利用 DataView.Sort 來實現

Dim dv as DataView = New DataView(DataTable)
dv.Sort = "排序的欄位"
Me.DataGrid.DataSource = dv
Me.DataGrid.DataBind()