C/s image operations

Source: Internet
Author: User

Read from database:
Method 1:
Sqlcommand cmd = new sqlcommand ("select * from student", mycn );
Sqldataadapter da = new sqldataadapter (CMD );
Dataset DS = new dataset ();
Da. Fill (DS, "student ");
Byte [] byteblobdata = new byte [0];
Byteblobdata = (byte []) (Ds. Tables ["student"]. Rows [Index] ["ZP"]);
Memorystream bytes blobdata = new memorystream (byteblobdata );
This. picturebox2.image = image. fromstream (describlobdata );

Method 2:
Oledbdataadapter myda = new oledbdataadapter ("select ZP from picture", mycn );
Dataset DS = new dataset ();
Myda. Fill (DS, "picture ");
Foreach (datarow rfirst in DS. Tables ["picture"]. Rows)
{
Bdata = (byte []) rfirst ["ZP"];
}

Memorystream stream = new memorystream (bdata, true );
// Memorystream stream = new memorystream (bdata );
// Create a bitmap from the stream
Bitmap BMP = new Bitmap (Stream );

Try
{
// Showing
This. picturebox1.image = BMP;
// This. picturebox1.image = image. fromstream (Stream );
}
Catch {};
// Close the stream
Stream. Close ();

========================================================== ======================================
Insert to database:
Sqlcommand mycmd = new sqlcommand ("insert into student (XH, ZP) values (@ XH, @ ZP)", mycn );

// Read JPG into file stream, and from there into byte array.
Filestream fsblobfile = new filestream (strblobfilepath, filemode. Open, fileaccess. Read );
Byte [] bytblobdata = new byte [fsblobfile. Length];
Fsblobfile. Read (bytblobdata, 0, bytblobdata. Length );
Fsblobfile. Close ();

Mycmd. Parameters. Add ("@ XH", this.txt box_xh.text.trim ());

// Create parameter for INSERT command and add to sqlcommand object.
Sqlparameter PRM = new sqlparameter ("@ ZP", sqldbtype. varbinary, bytblobdata. length, parameterdirection. Input, false, 0, 0, null, datarowversion. Current, bytblobdata );
Mycmd. Parameters. Add (PRM );


try
{< br> mycn. open ();
mycmd. executenonquery ();
MessageBox. show ("saved to the database! "," Prompt ");
}< br> catch (exception ex)
{< br> MessageBox. show (ex. message);
}< br> mycn. close ();

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.