Вы находитесь на странице: 1из 5

Imports System.Collections.

Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports System.Data.SqlClient
Imports System.IO

Namespace sqlFileExample
Public Partial Class frmMain
Inherits Form
Private objConn As New SqlConnection()
Private strSqlConn As String = "Data Source=localhost\sqlexpress;Initial
Catalog=db2;Integrated Security=True"

Private strQuery_AllAttachments As String = "select [id], [fileName], [fileSize] from


[tblAttachments] order by [fileName]"
Private strQuery_GetAttachmentById As String = "select * from [tblAttachments] where [id]
= @attachId"
Private strQuery_AllAttachments_AllFields As String = "select * from [tblAttachments]"

Public Sub New()


InitializeComponent()

'prevent resize at runtime


Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
Me.MaximizeBox = False
Me.MinimizeBox = False

Me.Text = "SQL file upload/download example"


End Sub

Private Sub frmMain_Load(sender As Object, e As EventArgs)


objConn.ConnectionString = strSqlConn
'set connection params
FillDataGrid(gridViewMain, strQuery_AllAttachments)
End Sub

Private Sub ConnectToDb()


'objConn.ConnectionString = strSqlConn; //set our connection params
'objConn.Open(); //open connection
End Sub

Private Sub frmMain_FormClosing(sender As Object, e As FormClosingEventArgs)


'objConn.Close(); //close connection
End Sub

Private Sub FillDataGrid(objGrid As DataGridView, strQuery As String)


Dim tbl1 As New DataTable()
Dim adapter1 As New SqlDataAdapter()
Dim cmd1 As New SqlCommand()
cmd1.Connection = objConn
' use connection object
cmd1.CommandText = strQuery
' set query to use
adapter1.MissingSchemaAction = MissingSchemaAction.AddWithKey

'grab schema
adapter1.SelectCommand = cmd1
'
adapter1.Fill(tbl1)
' fill the data table as specified
objGrid.DataSource = tbl1
' set the grid to display data
End Sub

Private Sub btnAddFile_Click(sender As Object, e As EventArgs)


If ofdMain.ShowDialog() <> DialogResult.Cancel Then
'upload the attachment
CreateAttachment(ofdMain.FileName)
End If
FillDataGrid(gridViewMain, strQuery_AllAttachments)
' refresh grid
End Sub

Private Sub CreateAttachment(strFile As String)


Dim objAdapter As New SqlDataAdapter(strQuery_AllAttachments_AllFields, objConn)
objAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey
Dim objCmdBuilder As New SqlCommandBuilder(objAdapter)
Dim objTable As New DataTable()
Dim objFileStream As New FileStream(strFile, FileMode.Open, FileAccess.Read)
Dim intLength As Integer = Convert.ToInt32(objFileStream.Length)
Dim objData As Byte()
objData = New Byte(intLength - 1) {}
Dim objRow As DataRow

Dim strPath As String() = strFile.Split(Convert.ToChar("\"))


objAdapter.Fill(objTable)

objFileStream.Read(objData, 0, intLength)
objFileStream.Close()

objRow = objTable.NewRow()
objRow("fileName") = strPath(strPath.Length - 1)
'clip the full path - we just want last part!
objRow("fileSize") = intLength / 1024
' KB instead of bytes
objRow("attachment") = objData
'our file
objTable.Rows.Add(objRow)
'add our new record
objAdapter.Update(objTable)
End Sub

Private Sub btnDownloadFile_Click(sender As Object, e As EventArgs)


SaveAttachment(sfdMain, gridViewMain)
FillDataGrid(gridViewMain, strQuery_AllAttachments)
' refresh grid
End Sub

Private Sub SaveAttachment(objSfd As SaveFileDialog, objGrid As DataGridView)


Dim strId As String = objGrid.SelectedRows(0).Cells("id").Value.ToString()
If Not String.IsNullOrEmpty(strId) Then
Dim sqlCmd As New SqlCommand(strQuery_GetAttachmentById, objConn)

sqlCmd.Parameters.AddWithValue("@attachId", strId)
Dim objAdapter As New SqlDataAdapter(sqlCmd)
Dim objTable As New DataTable()
Dim objRow As DataRow
objAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey
Dim sqlCmdBuilder As New SqlCommandBuilder(objAdapter)
objAdapter.Fill(objTable)
objRow = objTable.Rows(0)

Dim objData As Byte()


objData = DirectCast(objRow("attachment"), Byte())

If objSfd.ShowDialog() <> DialogResult.Cancel Then


Dim strFileToSave As String = objSfd.FileName
Dim objFileStream As New FileStream(strFileToSave, FileMode.Create,
FileAccess.Write)
objFileStream.Write(objData, 0, objData.Length)
objFileStream.Close()
End If
End If
End Sub
End Class
End Namespace

Вам также может понравиться