C # operate the varbinary (max) Field

Source: Internet
Author: User

If you store the photos directly in the SQL Server database, we recommend that you use the varbinary (max) field. The following code demonstrates how to use C # To operate the varbinary (max) field.

1. Add a record

Private void btnbrowse_click (Object sender, eventargs e) // view the photo
{
Openfiledialog DLG = new openfiledialog ();
DLG. Filter = "*. jpg (JPG file) | *. jpg | *. gif | *. GIF ";
DLG. filterindex = 1;
If (DLG. showdialog () = dialogresult. OK)
{
Textbox3.text = DLG. filename;
Picturebox1.image = image. fromfile (DLG. filename );
}
}

// Add a record

Using (sqlconnection conn = new sqlconnection (configurationmanager. connectionstrings ["DB"]. connectionstring ))
{
String SQL = "insert into EMP (name, age, photo) values (@ name, @ age, @ photo )";
Sqlcommand cmd = new sqlcommand (SQL, Conn );
Cmd. Parameters. addwithvalue ("@ name", textbox1.text );
Cmd. Parameters. addwithvalue ("@ age", convert. toint32 (textbox2.text ));
// Byte [] B;
// Using (filestream FS = new filestream (textbox3.text, filemode. Open, fileaccess. Read ))
//{
// B = new byte [fs. Length];
// Fs. Read (B, 0, (INT) fs. Length );
//}
Byte [] B;
If (textbox3.text! = "")
{
B = file. readallbytes (textbox3.text );
Cmd. Parameters. addwithvalue ("@ photo", B );
}
Else
{
Cmd. Parameters. addwithvalue ("@ photo", system. Data. sqltypes. sqlbinary. null );
}
Conn. open ();
Try
{
Cmd. executenonquery ();
}
Catch (exception ex)
{

MessageBox. Show (ex. Message );
}

}

2. Display record information and provide the modification function

Private void formdetail_load (Object sender, eventargs e) // display record details
{
Using (sqlconnection conn = new sqlconnection (configurationmanager. connectionstrings ["DB"]. connectionstring ))
{
String SQL = "select * from EMP where id = @ ID ";
Sqlcommand cmd = new sqlcommand (SQL, Conn );
Cmd. Parameters. addwithvalue ("@ ID", ID );
Conn. open ();
Using (sqldatareader DR = cmd. executereader ())
{
If (dr. Read ())
{
Textbox1.text = Dr [1]. tostring ();
Textbox2.text = Dr [2]. tostring ();
If (! Dr. isdbnull (3) // prevent empty photo Fields
{
System. Data. sqltypes. sqlbytes bytes = dr. getsqlbytes (3 );
Picturebox1.image = image. fromstream (bytes. Stream); // display a photo
}
}
}
}
}

Private void btnsave_click (Object sender, eventargs e) // update record
{
Using (sqlconnection conn = new sqlconnection (configurationmanager. connectionstrings ["DB"]. connectionstring ))
{
Byte [] B;
If (textbox3.text! = "") // You need to update the photo.
{
String SQL = "Update EMP set name = @ name, age = @ age, photo = @ photo where id = @ ID ";
Sqlcommand cmd = new sqlcommand (SQL, Conn );
Cmd. Parameters. addwithvalue ("@ name", textbox1.text );
Cmd. Parameters. addwithvalue ("@ age", convert. toint32 (textbox2.text ));
Cmd. Parameters. addwithvalue ("@ ID", ID );
B = file. readallbytes (textbox3.text );
Cmd. Parameters. addwithvalue ("@ photo", B );
Conn. open ();
Try
{
Cmd. executenonquery ();
}
Catch (exception ex)
{

MessageBox. Show (ex. Message );
}
}
Else // do not need to update the photo
{
String SQL = "Update EMP set name = @ name, age = @ age where id = @ ID ";
Sqlcommand cmd = new sqlcommand (SQL, Conn );
Cmd. Parameters. addwithvalue ("@ name", textbox1.text );
Cmd. Parameters. addwithvalue ("@ age", convert. toint32 (textbox2.text ));
Cmd. Parameters. addwithvalue ("@ ID", ID );
Conn. open ();
Try
{
Cmd. executenonquery ();
}
Catch (exception ex)
{

MessageBox. Show (ex. Message );
}
}
}
}

Related Article

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.