【发布时间】:2014-11-15 05:00:52
【问题描述】:
我打算在数据库中插入图像。但是插入时存在一些问题,即(图像格式:输入字符串格式不正确)。请帮我 。提前致谢。 此行出现错误 -> int count = cmd.ExecuteNonQuery();
我已经使用 img(列名)Blob(数据类型)创建了数据库。
public partial class check1 : System.Web.UI.Page
{
MySqlConnection con = new MySqlConnection("server=localhost; database=esample; uid=root;");
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridData();
}
}
protected void btnupload_Click(object sender, EventArgs e)
{
if (fileupload.HasFile)
{
int length = fileupload.PostedFile.ContentLength;
byte[] imgbyte = new byte[length];
HttpPostedFile img = fileupload.PostedFile;
img.InputStream.Read(imgbyte, 0, length);
string imagename = imgname.Text;
con.Open();
MySqlCommand cmd = new MySqlCommand("INSERT INTO brand (imgname,img) VALUES (@imagename,@imagedata)", con);
cmd.Parameters.Add("@imagename", SqlDbType.VarChar).Value = imagename;
cmd.Parameters.Add("@imagedata", SqlDbType.Blob).Value = imgbyte;
int count = cmd.ExecuteNonQuery();
con.Close();
if(count==1)
{
BindGridData();
imgname.Text = string.Empty;
ScriptManager.RegisterStartupScript(this, this.GetType(), "alertmessage", "javascript:alert('" + imagename + " image inserted successfully')", true);
}
}
}
private void BindGridData()
{
MySqlConnection con = new MySqlConnection("server=localhost; database=esample; uid=root;");
MySqlCommand command = new MySqlCommand("SELECT imgname,img,bid from brand", con);
MySqlDataAdapter daimages = new MySqlDataAdapter(command);
DataTable dt = new DataTable();
daimages.Fill(dt);
gvImages.DataSource = dt;
gvImages.DataBind();
gvImages.Attributes.Add("bordercolor", "black");
}
}
【问题讨论】:
-
imagename和imgbyte的值是多少?您确定将字节数组值保留在Image列类型中是个好主意吗?添加参数后你的完整cmd是多少? -
图片名称是从用户@runtime获取的文本