An example of SQLite is to store images in the SQLite database.

Source: Internet
Author: User

I recently learned about SQLite. After reading some documents, I wrote an example. It may be useful for beginners of SQLite or learning to insert images into the database. I will release the source code for reference. You are also welcome to make suggestions.
It contains common SQL operations, including addition, deletion, modification, and query. It is also described in the example that SQL statements of sqsag use parameters, which can be @ or $.
1. Demo using:
String ext = system. Io. Path. getextension (myfile. filename). tolower ();
If (EXT = ". jpg "| ext = ". BMP "| ext = ". JPEG "| ext = ". GIF "| ext = ". PNG "| ext = ". tiff "| ext = ". JPEG ")
{
String SQL = "insert into files (ID, [filename], filetype, filelength, filebyte, uploaddate, downloadcount, remark) values (@ ID, @ filename, @ filetype, @ filelength, @ filebyte, @ uploaddate, @ downloadcount, @ remark )";
Sqlitecommand cmd = new sqlitecommand (SQL, new sqliteconnection (datasouce ));
Cmd. Parameters. addwithvalue ("@ ID", guid. newguid (). tostring ());
Cmd. Parameters. addwithvalue ("@ FILENAME", myfile. filename );
Cmd. Parameters. addwithvalue ("@ filetype", myfile. postedfile. contenttype );
Cmd. Parameters. addwithvalue ("@ filelength", myfile. postedfile. contentlength. tostring ());
Cmd. Parameters. addwithvalue ("@ filebyte", myfile. filebytes );
Cmd. Parameters. addwithvalue ("@ uploaddate", system. datetime. Now. tostring ("yyyy-mm-dd hh: mm: SS "));
Cmd. Parameters. addwithvalue ("@ downloadcount", 0 );
Cmd. Parameters. addwithvalue ("@ remark", String. Empty );

Try
{
Cmd. Connection. open ();
If (CMD. executenonquery ()> 0)
{
Registerstartupscript ("alert", "<SCRIPT> alert ('upload successful! ') </SCRIPT> ");
}
Else
{
Registerstartupscript ("alert", "<SCRIPT> alert ('upload failed! ') </SCRIPT> ");
}
}
Catch (sqliteexception ex)
{
Console. Write (ex. Message );
}
Finally
{
Cmd. Connection. Close ();
}
}
Else
{
Registerstartupscript ("alert", "<SCRIPT> alert ('The photo format is incorrect. Please reselect it! ') </SCRIPT> ");
}

2. $ Demo:
String SQL = "update files set downloadcount = downloadcount + 1 where id = $ id ";
Sqlitecommand cmd = new sqlitecommand (SQL, new sqliteconnection (datasouce ));
Cmd. Parameters. addwithvalue ("$ id", fileid );
Try
{
Cmd. Connection. open ();
Cmd. executenonquery ();
}
Catch (exception ex)
{
Console. Write (ex. Message );
}
Finally
{
Cmd. Connection. Clone ();
}

I personally think SQLite is quite good, at least better than access. Some of the applications I personally see are the China Mobile Feixin client. Program Google Chrome.

download source code: storeimage.zip

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.