2007-01-22

將圖片存進DB中

Dim con As New SqlConnection _
       ("Server=YourServer;uid=<username>;pwd=<strong password>;database=northwind")
      Dim da As New SqlDataAdapter _
       ("Select * From MyImages", con)
      Dim MyCB As SqlCommandBuilder = New SqlCommandBuilder(da)
      Dim ds As New DataSet()
      da.MissingSchemaAction = MissingSchemaAction.AddWithKey
      Dim fs As New FileStream _
       ("C:\winnt\Gone Fishing.BMP", FileMode.OpenOrCreate, _
        FileAccess.Read)
      Dim MyData(fs.Length) As Byte
      fs.Read(MyData, 0, fs.Length)
      fs.Close()
      con.Open()
      da.Fill(ds, "MyImages")
      Dim myRow As DataRow
      myRow = ds.Tables("MyImages").NewRow()
      myRow("Description") = "This would be description text"
      myRow("imgField") = MyData
      ds.Tables("MyImages").Rows.Add(myRow)
      da.Update(ds, "MyImages")
      fs = Nothing
      MyCB = Nothing
      ds = Nothing
      da = Nothing
      con.Close()
      con = Nothing
      MsgBox ("Image saved to database")

沒有留言:

張貼留言