gpt4 book ai didi

c# - 无法更新 sql server 中的 varbinary(max) 列

转载 作者:太空宇宙 更新时间:2023-11-03 16:12:39 25 4
gpt4 key购买 nike

<分区>

我正在尝试使用 varbinary (SQL Server 2008) 更新我的个人资料图片。它似乎没有更新我放在文件上传中的图片。然后我尝试将我的个人资料图片列设置为 NULL 它似乎也不起作用。下面是我用来将个人资料图片列更新为 NULL 并使用 fileupload 更新个人资料图片并将其转换为 varbinary(MAX) 的代码。请帮我看看我做错了什么。谢谢!

    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);

string strQuery = "Update LoginRegisterOthers Set profilepic = NULL Where username = '" + username + "'";
SqlCommand cmd = new SqlCommand(strQuery);
InsertUpdateData(cmd);

//insert the file into database
//string strQuery2 = "Update LoginRegisterOthers Set profilepic = @Data Where username = @Username";
//SqlCommand cmd2 = new SqlCommand(strQuery2);
//cmd2.Parameters.Add("@Data", SqlDbType.Binary).Value = bytes;
//cmd2.Parameters.Add("@Username", SqlDbType.VarChar).Value = username;
//InsertUpdateData(cmd2);
//lblUpload.ForeColor = System.Drawing.Color.Green;
//lblUpload.Text = "Profile Updated.";

Response.Redirect("MemberProfile.aspx");
}
else if (contenttype == String.Empty)
{
lblUpload.Text = "Please select your image before uploading!";
}
else
{
lblUpload.ForeColor = System.Drawing.Color.Red;
lblUpload.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();
}
}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com