' VB Imports System.Data in .NET

Drawing QR-Code in .NET ' VB Imports System.Data

' VB Imports System.Data
QR Code ISO/IEC18004 Generation In .NET
Using Barcode maker for ASP.NET Control to generate, create Quick Response Code image in ASP.NET applications.
www.OnBarcode.com
Barcode Generator In Visual Studio .NET
Using Barcode generation for ASP.NET Control to generate, create barcode image in ASP.NET applications.
www.OnBarcode.com
Imports System.Data.SqlClient
QR-Code Maker In Visual C#.NET
Using Barcode creation for .NET Control to generate, create QR Code ISO/IEC18004 image in .NET applications.
www.OnBarcode.com
QR Code Maker In VS .NET
Using Barcode creator for .NET framework Control to generate, create QR Code ISO/IEC18004 image in Visual Studio .NET applications.
www.OnBarcode.com
Imports System.IO
QR-Code Printer In Visual Basic .NET
Using Barcode creator for VS .NET Control to generate, create QR Code 2d barcode image in Visual Studio .NET applications.
www.OnBarcode.com
Painting PDF417 In .NET
Using Barcode drawer for ASP.NET Control to generate, create PDF-417 2d barcode image in ASP.NET applications.
www.OnBarcode.com
Public Class Form1 Private NorthwindConnection As New SqlConnection _ ("Data Source=.\sqlexpress;Initial Catalog=Northwind;Integrated Security=True") Private CompleteFilePath As String = "" Private SavePath As String = "" Private Sub GetCompleteFilePath()
Bar Code Generation In Visual Studio .NET
Using Barcode generator for ASP.NET Control to generate, create bar code image in ASP.NET applications.
www.OnBarcode.com
Drawing Code 39 Extended In .NET
Using Barcode creation for ASP.NET Control to generate, create Code 39 image in ASP.NET applications.
www.OnBarcode.com
Dim OpenDialog As New OpenFileDialog
Encode GS1 - 13 In .NET Framework
Using Barcode creator for ASP.NET Control to generate, create UPC - 13 image in ASP.NET applications.
www.OnBarcode.com
EAN128 Maker In .NET
Using Barcode encoder for ASP.NET Control to generate, create UCC-128 image in ASP.NET applications.
www.OnBarcode.com
OpenDialog.Title = "Select Document File to Save"
Generate Matrix Barcode In Visual Studio .NET
Using Barcode encoder for ASP.NET Control to generate, create Matrix Barcode image in ASP.NET applications.
www.OnBarcode.com
MSI Plessey Encoder In .NET Framework
Using Barcode creation for ASP.NET Control to generate, create MSI Plessey image in ASP.NET applications.
www.OnBarcode.com
OpenDialog.ShowDialog()
Making EAN128 In VB.NET
Using Barcode generation for .NET Control to generate, create UCC-128 image in VS .NET applications.
www.OnBarcode.com
Paint Code-128 In VS .NET
Using Barcode maker for Reporting Service Control to generate, create Code 128A image in Reporting Service applications.
www.OnBarcode.com
CompleteFilePath = OpenDialog.FileName
Recognize Bar Code In None
Using Barcode scanner for Software Control to read, scan read, scan image in Software applications.
www.OnBarcode.com
Data Matrix ECC200 Drawer In VB.NET
Using Barcode creator for .NET Control to generate, create ECC200 image in Visual Studio .NET applications.
www.OnBarcode.com
End Sub Private Sub GetSavePath()
USS Code 128 Generation In Java
Using Barcode maker for Android Control to generate, create Code 128 Code Set A image in Android applications.
www.OnBarcode.com
PDF-417 2d Barcode Recognizer In Java
Using Barcode reader for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
Dim SavePathDialog As New FolderBrowserDialog
PDF 417 Encoder In .NET Framework
Using Barcode creation for Reporting Service Control to generate, create PDF 417 image in Reporting Service applications.
www.OnBarcode.com
Code 3/9 Generation In Java
Using Barcode printer for Java Control to generate, create Code39 image in Java applications.
www.OnBarcode.com
SavePathDialog.Description = "Select a folder to restore BLOB file to"
SavePathDialog.ShowDialog()
SavePath = SavePathDialog.SelectedPath
End Sub ' Create a table to hold our BLOB values
Private Sub CreateDocumentStorageTable()
Dim CreateTableCommand As New SqlCommand
CreateTableCommand.Connection = NorthwindConnection
CreateTableCommand.CommandType = CommandType.Text
6
Working with Data in a Connected Environment
CreateTableCommand.CommandText = "IF OBJECT_ID ( 'DocumentStorage' ) IS NOT NULL " & _ "DROP TABLE DocumentStorage; " & _ "CREATE TABLE DocumentStorage(" & _ "DocumentID int IDENTITY(1,1) NOT NULL, " & _ "FileName nvarchar(255) NOT NULL, " & _ "DocumentFile varbinary(max) NOT NULL)" CreateTableCommand.Connection.Open()
CreateTableCommand.ExecuteNonQuery()
CreateTableCommand.Connection.Close()
End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim response As DialogResult = MessageBox.Show("Create the Document Storage Table " & _ Environment.NewLine & "Click Yes to create a new DocumentStorage table. Click No if you already have one!", _ "Create DocumentStorage table", MessageBoxButtons.YesNo, _ MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) Select Case response Case Is = Windows.Forms.DialogResult.Yes CreateDocumentStorageTable() Case Is = Windows.Forms.DialogResult.No refreshBlobList() End Select End Sub Private Sub refreshBlobList() Dim GetBlobListCommand As New SqlCommand _ ("SELECT FileName FROM DocumentStorage", NorthwindConnection) Dim reader As SqlDataReader GetBlobListCommand.Connection.Open()
reader = GetBlobListCommand.ExecuteReader
While reader.Read
BlobList.Items.Add(reader(0))
End While
reader.Close()
GetBlobListCommand.Connection.Close()
BlobList.SelectedIndex = 0 End Sub End Class // C# using using using using using using
System; System.Collections.Generic; System.ComponentModel; System.Data; System.Drawing; System.Text;
Lesson 3: Saving and Retrieving BLOB Values in a Database
using System.Windows.Forms; using System.Data.SqlClient; using System.IO; namespace BlobSampleCS { public partial class Form1 : Form { public Form1()
InitializeComponent();
private SqlConnection NorthwindConnection = new SqlConnection ("Data Source=.\\sqlexpress;Initial Catalog=Northwind;Integrated Security=True"); private String CompleteFilePath = ""; private String SavePath = ""; private void GetCompleteFilePath() {
OpenFileDialog OpenDialog = new OpenFileDialog();
OpenDialog.Title = "Select Document to Save";
OpenDialog.ShowDialog();
CompleteFilePath = OpenDialog.FileName;
} private void GetSavePath() { FolderBrowserDialog SavePathDialog = new FolderBrowserDialog(); SavePathDialog.Description = "Select a folder to restore BLOB file to"; SavePathDialog.ShowDialog(); SavePath = SavePathDialog.SelectedPath; } // Create a table to hold our BLOB values.
private void CreateDocumentStorageTable()
SqlCommand CreateTableCommand = new SqlCommand(); CreateTableCommand.Connection = NorthwindConnection; CreateTableCommand.CommandType = CommandType.Text; CreateTableCommand.CommandText = "IF OBJECT_ID ( 'DocumentStorage' ) IS NOT NULL " +
"DROP TABLE DocumentStorage; " +
"CREATE TABLE DocumentStorage(" +
"DocumentID int IDENTITY(1,1) NOT NULL, " +
"FileName nvarchar(255) NOT NULL, " +
"DocumentFile varbinary(max) NOT NULL)";
CreateTableCommand.Connection.Open();
CreateTableCommand.ExecuteNonQuery();
CreateTableCommand.Connection.Close();
6
Working with Data in a Connected Environment
private void Form1_Load(object sender, EventArgs e) { DialogResult response = MessageBox.Show("Create the Document Storage Table " + Environment.NewLine + "Click Yes to create a new DocumentStorage table." + "Click No if you already have one!", "Create DocumentStorage table", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2); switch (response)
case DialogResult.Yes: CreateDocumentStorageTable(); break; case DialogResult.No: RefreshBlobList(); break; } } private void RefreshBlobList() { SqlCommand GetBlobListCommand = new SqlCommand ("SELECT FileName FROM DocumentStorage", NorthwindConnection); SqlDataReader reader; GetBlobListCommand.Connection.Open();
reader = GetBlobListCommand.ExecuteReader();
while (reader.Read())
BlobList.Items.Add(reader[0]); } reader.Close(); GetBlobListCommand.Connection.Close(); BlobList.SelectedIndex = 0; } }
8. Add the following code to save the BLOB to the database:
' VB Private Sub SaveBlobToDatabase() ' This call lets you select the
' binary file to save As a BLOB
' in the database.
GetCompleteFilePath()
' The BLOB holds the byte array to save.
Dim BLOB() As Byte
Lesson 3: Saving and Retrieving BLOB Values in a Database
' The FileStream is the stream of bytes
' that represent the binary file.
Dim FileStream As New IO.FileStream _
(CompleteFilePath, IO.FileMode.Open, IO.FileAccess.Read)
' The reader reads the binary data from the FileStream.
Dim reader As New IO.BinaryReader(FileStream)
' The BLOB is asigned the bytes from the reader.
' The file length is passed to the ReadBytes method
' telling it how many bytes to read.
BLOB=
reader.ReadBytes(CInt(My.Computer.FileSystem.GetFileInfo(CompleteFilePath).Length)) FileStream.Close()
reader.Close()
' Create a command object to save
' the BLOB value.
Dim SaveDocCommand As New SqlCommand
SaveDocCommand.Connection = NorthwindConnection
SaveDocCommand.CommandText = "INSERT INTO DocumentStorage" & _
"(FileName, DocumentFile)" & _ "VALUES (@FileName, @DocumentFile)" ' Create parameters to store the filename and BLOB data.
Dim FileNameParameter As New SqlParameter("@FileName", SqlDbType.NChar)
Dim DocumentFileParameter As New SqlParameter("@DocumentFile", SqlDbType.Binary)
SaveDocCommand.Parameters.Add(FileNameParameter)
SaveDocCommand.Parameters.Add(DocumentFileParameter)
' Parse the filename out of the complete path
' and assign it to the parameter.
FileNameParameter.Value = _
CompleteFilePath.Substring(CompleteFilePath.LastIndexOf("\") + 1) ' Set the DocumentFile parameteter to the BLOB Value.
DocumentFileParameter.Value = BLOB
' Execute the command and save the BLOB to the database. Try
SaveDocCommand.Connection.Open()
SaveDocCommand.ExecuteNonQuery()
MessageBox.Show(FileNameParameter.Value.ToString & _
" saved to database.", "BLOB Saved!", MessageBoxButtons.OK, _ MessageBoxIcon.Information) Catch ex As Exception MessageBox.Show(ex.Message, "Save Failed", _ MessageBoxButtons.OK, MessageBoxIcon.Error) Finally SaveDocCommand.Connection.Close() End Try End Sub
6
Working with Data in a Connected Environment
// C# private void SaveBlobToDatabase() { // This call lets you select the // binary file to save As a BLOB // in the database. GetCompleteFilePath(); // The BLOB holds the byte array to save.
byte[] BLOB;
// The FileStream is the stream of bytes
// that represent the binary file.
System.IO.FileStream FileStream = new System.IO.FileStream _
(CompleteFilePath, System.IO.FileMode.Open, System.IO.FileAccess.Read); // The reader reads the binary data from the FileStream.
System.IO.BinaryReader reader = new System.IO.BinaryReader(FileStream);
// The BLOB is asigned the bytes from the reader.
// The file length is passed to the ReadBytes method
// telling it how many bytes to read.
System.IO.FileInfo file = new FileInfo(CompleteFilePath); BLOB = reader.ReadBytes((int)(file.Length)); FileStream.Close();
reader.Close();
// Create a command object to save
// the BLOB value.
SqlCommand SaveDocCommand = new SqlCommand();
SaveDocCommand.Connection = NorthwindConnection;
SaveDocCommand.CommandText = "INSERT INTO DocumentStorage" +
"(FileName, DocumentFile)" + "VALUES (@FileName, @DocumentFile)"; // Create parameters to store the filename and BLOB data.
SqlParameter FileNameParameter = new SqlParameter("@FileName", SqlDbType.NChar);
SqlParameter DocumentFileParameter = new SqlParameter _
("@DocumentFile", SqlDbType.Binary);
SaveDocCommand.Parameters.Add(FileNameParameter);
SaveDocCommand.Parameters.Add(DocumentFileParameter);
Copyright © OnBarcode.com . All rights reserved.