MYSQL blob picture type storage bug Resolution

Source: Internet
Author: User

First installed in the local MySQL, and installed a Navicat management tool, and a new project to test:
The simple test method is to put a button, click, read the remote picture, and then save to the database, and then read out to display the interface.
The management tool randomly built a table "D1" and got three id,img,name three fields.

The example code is as follows, because the code is not written too canonical because of the test:

protected void btnTest_Click (object sender, EventArgs e)
String url = "Baidu_jgylogo3.gif";
BOOL result = FALSE;
int id = 0;
String conn = "host=;" port=3306;database=test;uid=root;pwd=123456 ";
String conn = "server=.; database=demo;uid=sa;pwd=123456 ";
using (maction action = new Maction ("D1", conn))
WebClient WC = new WebClient ();
if (action. Fill (2))
byte[] data = WC. Downloaddata (URL);
Action. Set ("img", data);
Action. Set ("name", "LDF");
result = action. Insert (;
id = action. Get<int> ("id");
catch (Exception err)
Log.writelogtotxt (ERR);
if (result)//open another read try
using (maction action = new Maction ("D1", conn))
if (action. Fill (ID))
Response.BinaryWrite (action. Get<byte[]> ("img"));


Solve and debug through:

A local test, debugging, finds that an exception was thrown when assigning a parameter to a BLOB type: The index exceeded the array bounds.
Abnormal too confused, online search, some people say is: Mysql.dll version of the problem, spent a csdn points under a 6.2, 3 version, or not.
Then unconsciously debugging, estimating the luck problem, unexpectedly let me find out the problem, can only say that is good luck.

Look at a small piece of code, the parameters of the assignment, parameter multiple database support, similar to this writing:

DbParameter para = _fac. CreateParameter ()//To obtain the parameters of the current database type through the factory advertisement
Para. ParameterName = parametername;
Para. Value = value = = null? DBNull.Value:value;
Para. DbType = DbType;

if (Size > 1)//Set length.
Para. size = size;
Para. Direction = Direction;

This code is actually very simple:

Produces a parameter, then assigns the parameter name, the parameter value, the parameter type, the parameter size and the parameter point.
MySQL actually has some bugs on the problem of assigning value to parameter size.

When I debug skip over to para. When the size is assigned, it is normal.

So the code changes to this:

if (dbType!= dbtype.binary && size >-1)//mysql cannot set the length, otherwise the index is reported to exceed the array bounds error.

Then I tested the other databases and found that the specified size was normal, and finally came to a conclusion:

Tested, other database types are normal to the size of the assignment, only the MySQL problem, for this, in the preparation of multiple database support framework, will always encounter a lot of problems, don't tangle, always to solve.

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: 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.