使用C#Windows应用程序将图像保存到MySQL [英] Saving Image to MySQL using C# Windows application

查看:92
本文介绍了使用C#Windows应用程序将图像保存到MySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好..

需要帮助从C#Windows应用程序上传图片.不幸的是我..我有错误->字符串或二进制数据将被截断.该语句已终止

试试
{
//将图像字节读入字节数组
byte [] imageSampleData = ReadImageToBytes(txtImagePath.Text);

//初始化SQL Server连接
SqlConnection con =新的SqlConnection(txtConnectionString.Text);

//设置插入查询
字符串查询="INSERT INTO p(ee,i1)values(@ ee,@ i1)";

//初始化SqlCommand对象以进行插入.
SqlCommand cmd =新的SqlCommand(query,con);

cmd.Parameters.Add(新SqlParameter("@ ee",
(object)txtImagePath.Text));

cmd.Parameters.Add(新SqlParameter("@ i1",
(object)imageSampleData));

//打开连接并执行插入查询.
con.Open();
cmd.ExecuteNonQuery(); --->错误发生在这里!
con.Close();


请帮助我.

Hi everyone..

Need help to upload picture from C# windows application. Unlucky me.. ive got error --> string or binary data would be truncated.the statement has been terminated

try
{
//Read Image Bytes into a byte array
byte[] imageSampleData = ReadImageToBytes(txtImagePath.Text);

//Initialize SQL Server Connection
SqlConnection con = new SqlConnection(txtConnectionString.Text);

//Set insert query
string query = "INSERT INTO p (ee,i1) values(@ee,@i1)";

//Initialize SqlCommand object for insert.
SqlCommand cmd = new SqlCommand(query, con);

cmd.Parameters.Add(new SqlParameter("@ee",
(object)txtImagePath.Text));

cmd.Parameters.Add(new SqlParameter("@i1",
(object)imageSampleData));

//Open connection and execute insert query.
con.Open();
cmd.ExecuteNonQuery();---> error occur here!
con.Close();


Please help me.

推荐答案

更改您的数据库定义!
查看根据图像数据的大小将"i1"字段设置为BLOB,MEDIUMBLOB或LONGBLOB.

另外,请大家帮个忙:使用易记的名称!

"p"不是一个好的表名.
"ee"不是一个好字段,"i1"也不是一个好字段.

这些名称说明它们包含的数据是什么?没事.
这些名称对数据库设计人员说了什么?他是个没有经验的白痴...:laugh:

[edit]此外,请查看mySQLCommand.AddWithValue方法-这将使您的代码更加整洁.顺便说一句,顺便说一句,使用参数化查询-大多数初学者都错过了这一点. OriginalGriff [/edit]
Change your database definition!
Look at making the "i1" field a BLOB, MEDIUMBLOB or LONGBLOB depending on the size of the image data.

Plus, do us all a favour: USE SENSIBLE NAMES!

"p" is not a good table name.
"ee" is not a good field, nor is "i1".

What do these names say about the data they contain? Nothing.
What do these names say about the database designer? That he is an inexperienced idiot... :laugh:

[edit]In addition, have a look at the mySQLCommand.AddWithValue method - it will make your code a lot cleaner. Well done, BTW, for using parametrised queries - most beginners miss that. OriginalGriff[/edit]


这些链接可以帮助您理解代码
解释也可用

http://guruparan.blog .com/2011/06/15/saving-and-retriving-images-from-mysql-databases-using-c-part1/ [ ^ ]

http://guruparan.blog .com/2011/06/15/saving-and-retriving-images-from-mysql-databases-using-c-part2/ [ ^ ]
These links can Help you understand the code
the explanation is also available

http://guruparan.blog.com/2011/06/15/saving-and-retriving-images-from-mysql-databases-using-c-part1/[^]

http://guruparan.blog.com/2011/06/15/saving-and-retriving-images-from-mysql-databases-using-c-part2/[^]


这篇关于使用C#Windows应用程序将图像保存到MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆