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=127.0.0.1;" 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 ();
Try
{
if (action. Fill (2))
//{
byte[] data = WC. Downloaddata (URL);
Action. Set ("img", data);
Action. Set ("name", "LDF");
result = action. Insert (insertop.id);
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.