无法使用VARBINARY更新资料图片 [英] unable to update profile picture using varbinary
本文介绍了无法使用VARBINARY更新资料图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图用更新我的VARBINARY资料图片(SQL Server 2008中)。它似乎并没有更新,我把在文件上传图片。下面是我用它来更新我的个人资料图片的代码。请帮我看看上我的编码的一部分,我做了错误。
感谢。
保护无效btnUpload_Click(对象发件人,EventArgs五)
{
字符串username =(字符串)会议[用户名];
字符串文件路径= FileUpload1.PostedFile.FileName;
字符串文件名= Path.GetFileName(文件路径);
串EXT = Path.GetExtension(文件名);
串的contentType =的String.Empty;
开关(EXT)
{
案.JPG:
的contentType =图像/ JPG
中断;
}
如果(=的contentType的String.Empty!)
{
流FS = FileUpload1.PostedFile.InputStream;
BinaryReader BR =新BinaryReader(FS);
字节[]字节= br.ReadBytes((Int32)已fs.Length);
//将文件插入数据库
串strQuery =更新LoginRegisterOthers集profilepic = @Data其中,username ='+用户名+';
的SqlCommand CMD =新的SqlCommand(strQuery);
cmd.Parameters.Add(@数据,SqlDbType.Binary).value的=字节;
InsertUpdateData(CMD);
lblMessage.ForeColor = System.Drawing.Color.Green;
lblMessage.Text =个人资料已更新。
的Response.Redirect(MemberProfile.aspx);
}
,否则如果(==的contentType的String.Empty)
{
lblMessage.Text =上传之前,请选择您的形象!
}
,否则
{
lblMessage.ForeColor = System.Drawing.Color.Red;
lblMessage.Text =文件格式无法识别。 +上传图片格式;
}
}
私人布尔InsertUpdateData(的SqlCommand CMD)
{
的SqlConnection CON =新的SqlConnection(数据源=本地主机;初始目录=项目;集成安全性=真);
cmd.CommandType = CommandType.Text;
cmd.Connection = CON;
试
{
con.Open();
cmd.ExecuteNonQuery();
返回真;
}
赶上(异常前)
{
的Response.Write(ex.Message);
返回FALSE;
}
终于
{
con.Close();
con.Dispose();
}
}
解决方案
尝试这样的:
...
//将文件插入数据库
串strQuery =更新LoginRegisterOthers集profilepic =(SELECT BULKCOLUMN FROM OPENROWSET(BULK N'+文件名+',SINGLE_BLOB)AS FIle_picture)其中,username ='+用户名+';
...
您可以直接与SQL加载文件。
I'm trying to update my profile picture using varbinary (SQL Server 2008). It don't seem to update the picture that I put in the fileupload. Below is the code I use to update my profile picture. Do help me take a look on which part of my coding did I do wrongly. Thanks.
protected void btnUpload_Click(object sender, EventArgs e)
{
String username = (String)Session["username"];
string filePath = FileUpload1.PostedFile.FileName;
string filename = Path.GetFileName(filePath);
string ext = Path.GetExtension(filename);
string contenttype = String.Empty;
switch (ext)
{
case ".jpg":
contenttype = "image/jpg";
break;
}
if (contenttype != String.Empty)
{
Stream fs = FileUpload1.PostedFile.InputStream;
BinaryReader br = new BinaryReader(fs);
Byte[] bytes = br.ReadBytes((Int32)fs.Length);
//insert the file into database
string strQuery = "Update LoginRegisterOthers Set profilepic = @Data Where username = '" + username + "'";
SqlCommand cmd = new SqlCommand(strQuery);
cmd.Parameters.Add("@Data", SqlDbType.Binary).Value = bytes;
InsertUpdateData(cmd);
lblMessage.ForeColor = System.Drawing.Color.Green;
lblMessage.Text = "Profile Updated.";
Response.Redirect("MemberProfile.aspx");
}
else if (contenttype == String.Empty)
{
lblMessage.Text = "Please select your image before uploading!";
}
else
{
lblMessage.ForeColor = System.Drawing.Color.Red;
lblMessage.Text = "File format not recognised." + " Upload Image formats";
}
}
private Boolean InsertUpdateData(SqlCommand cmd)
{
SqlConnection con = new SqlConnection("Data Source=localhost; Initial Catalog=project; Integrated Security=True");
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
try
{
con.Open();
cmd.ExecuteNonQuery();
return true;
}
catch (Exception ex)
{
Response.Write(ex.Message);
return false;
}
finally
{
con.Close();
con.Dispose();
}
}
解决方案
try this:
...
//insert the file into database
string strQuery = "Update LoginRegisterOthers Set profilepic = (SELECT BULKCOLUMN FROM OPENROWSET(BULK N'"+filename+"', SINGLE_BLOB) AS FIle_picture) Where username = '" + username + "'";
...
You can load a file directly with sql.
这篇关于无法使用VARBINARY更新资料图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文