原來~ 要在 Visual Studio 裡使用 DB2 Connection 來連結資料庫
一定要先裝好 DB2 後再安裝 Visual Studio
這樣 VS 就會自動將 DB2 工具列加進來了!
20091007補充:
其實只要先裝好資料庫再裝 VS
VS 就會自動將資料庫工具列加入!
2009年9月30日 星期三
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中上傳檔案到資料庫
因為專案裡所使用的資料庫為 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中上傳檔案到資料庫
訂閱:
意見 (Atom)
