Accessing Files Through ADO. NET

Source: Internet
Author: User

Sometimes we need to store some large data objects, executable files, videos and documents into the database. In ms SQL Server, the Image data type is used to store up to 2 GB of data. The following is a small example of using ADO. NET and ms SQL Server.
Create a test data table first.
Enter and execute the following statement in the query Analyzer:
Create table [imgtable] (
[Imgid] [int] IDENTITY (1, 1) not null,
[Imgname] [varchar] (100) COLLATE Chinese_PRC_CI_AS NULL,
[ImgData] [image] NULL,
PRIMARY KEY CLUSTERED
(
[Imgid]
) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
This requires an additional table named imgtable in the database you selected.
The code in VS is as follows:
Using System;
Using System. Drawing;
Using System. Collections;
Using System. ComponentModel;
Using System. Windows. Forms;
Using System. Data;
Using System. Data. SqlClient;
Using System. IO;
Namespace ADO_Demo
{
/// <Summary>
/// Summary of Form1.
/// </Summary>
Public class ADO_Demo: System. Windows. Forms. Form
{
Private System. Windows. Forms. Button button1;
Private System. Windows. Forms. Button button2;
Private System. Windows. Forms. PictureBox pictureBox1;
Private System. Windows. Forms. OpenFileDialog openFileDialog1;
Private System. Windows. Forms. Button button3;
/// <Summary>
/// Required designer variables.
/// </Summary>
Private System. ComponentModel. Container components = null;
Public ADO_Demo ()
{
//
// Required for Windows Form Designer support
//
InitializeComponent ();
//
// TODO: add Any constructor code after InitializeComponent calls
//
}
/// <Summary>
/// Clear all resources in use.
/// </Summary>
Protected override void Dispose (bool disposing)
{
If (disposing)
{
If (components! = Null)
{
Components. Dispose ();
}
}
Base. Dispose (disposing );
}

# Region code generated by Windows Form Designer
/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void InitializeComponent ()
{
This. button1 = new System. Windows. Forms. Button ();
This. button2 = new System. Windows. Forms. Button ();
This. pictureBox1 = new System. Windows. Forms. PictureBox ();
This. openFileDialog1 = new System. Windows. Forms. OpenFileDialog ();
This. button3 = new System. Windows. Forms. Button ();
This. SuspendLayout ();
//
// Button1
//
This. button1.Location = new System. Drawing. Point (368, 48 );
This. button1.Name = "button1 ";
This. button1.Size = new System. Drawing. Size (104, 23 );
This. button1.TabIndex = 0;
This. button1.Text = "Save image ";
This. button1.Click + = new System. EventHandler (this. button#click );
//
// Button2
//
This. button2.Location = new System. Drawing. Point (368,120 );
This. button2.Name = "button2 ";
This. button2.Size = new System. Drawing. Size (104, 23 );
This. button2.TabIndex = 1;
This. button2.Text = "show image ";
This. button2.Click + = new System. EventHandler (this. button2_Click );
//
// PictureBox1
//
This. pictureBox1.Location = new System. Drawing. Point (8, 16 );
This. pictureBox1.Name = "pictureBox1 ";
This. pictureBox1.Size = new System. Drawing. Size (312,288 );
This. pictureBox1.TabIndex = 2;
This. pictureBox1.TabStop = false;
//
// OpenFileDialog1
//
This. openFileDialog1.FileOk + = new System. ComponentModel. CancelEventHandler (this. openFileDialog1_FileOk );
//
// Button3
//
This. button3.Location = new System. Drawing. Point (368,200 );
This. button3.Name = "button3 ";
This. button3.Size = new System. Drawing. Size (104, 23 );
This. button3.TabIndex = 1;
This. button3.Text = "read and open the file ";
This. button3.Click + = new System. EventHandler (this. button3_Click );
//
// ADO_Demo
//
This. AutoScaleBaseSize = new System. Drawing. Size (6, 14 );
This. ClientSize = new System. Drawing. Size (496,317 );
This. Controls. Add (this. pictureBox1 );
This. Controls. Add (this. button2 );
This. Controls. Add (this. button1 );
This. Controls. Add (this. button3 );
This. Name = "ADO_Demo ";
This. Text = "ADO_Demo ";
This. ResumeLayout (false );
}
# Endregion
/// <Summary>
/// Main entry point of the application.
/// </Summary>
[STAThread]
Static void Main ()
{
Application. Run (new ADO_Demo ());
}
/// <Summary>
/// Click the OK button in the open file dialog box to save the file to the database.
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
Private void openFileDialog1_FileOk (object sender, System. ComponentModel. CancelEventArgs e)
{
String filename = this. openFileDialog1.FileName;
SqlConnection conn = new SqlConnection ("server = 192.168.2.200; integrated security = sspi; database = northwind ");
SqlCommand cmd = new SqlCommand ("insert imgtable values (@ imgname, @ imgData)", conn );
SqlParameter pm = new SqlParameter ("@ imgname", SqlDbType. VarChar, 100 );
Pm. Value = filename;
SqlParameter pm1 = new SqlParameter ("@ imgData", SqlDbType. Image );
FileStream fs = new FileStream (filename, FileMode. Open );
Int len = (int) fs. Length;
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.