在 SQL 中使用 FileUpload 在 ASP.NET 中更新图像

Update image in SQL using FileUpload in ASP.NET

我正在尝试为 SQL 中的用户存储个人资料图片。我的问题是数据库中没有保存任何内容 table。我要保存图像的列的数据类型为 image

这是我的代码:

更新:代码已更新且有效。问题是查询中使用的参数。非常感谢 SeM 和 Crowcoder!

protected void ButtonPic_Click(object sender, EventArgs e)
    {
        string filePath = FileUpload1.PostedFile.FileName;
        string filename = Path.GetFileName(filePath);
        string ext = Path.GetExtension(filename);
        string contenttype = string.Empty;
        string username = User.Identity.Name; // Gets the current username

        switch (ext)
        {
            case ".jpg":
                contenttype = "image/jpg";
                break;
            case ".png":
                contenttype = "image/png";
                break;
        }

        if (contenttype != string.Empty)
        {
            Stream fs = FileUpload1.PostedFile.InputStream;
            BinaryReader br = new BinaryReader(fs);
            byte[] bytes = br.ReadBytes((int)fs.Length + 1);

            string base64String = Convert.ToBase64String(bytes, 0,   bytes.Length);
            Image1.ImageUrl = "data:image/png;base64," + base64String;

            string constr = ConfigurationManager.ConnectionStrings["LoginDBConnectionString"].ConnectionString;
            string strQuery = "UPDATE Users SET Image=@data WHERE Username=@user";

            try
            {
                using (SqlConnection con = new SqlConnection(constr))
                {
                    con.Open();
                    using (SqlCommand cmd = con.CreateCommand())
                    {
                        cmd.CommandType = CommandType.Text;
                        cmd.Parameters.Add(new SqlParameter("@data", bytes)); 
                        cmd.Parameters.Add(new SqlParameter("@user", username));
                        cmd.CommandText = strQuery;
                        cmd.ExecuteNonQuery();
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }
        else
        {
            lblMessage.ForeColor = System.Drawing.Color.Red;
            lblMessage.Text = "Filformat stöds inte." +
              " Endast jpg/png är tillåtet";
        }
    }

字节数组的转换工作正常,但我不知道问题出在哪里。

删除 using(SqlDataAdapter sda = new SqlDataAdapter()){},然后像这样更改该部分:

try
{
    using (SqlConnection con = new SqlConnection(constr))
    {
        con.Open();
        using (SqlCommand cmd = con.CreateCommand())
        {
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.Add(new SqlParameter("@data", bytes));
            cmd.Parameters.Add(new SqlParameter("@user", LoginName1.ToString()));
            cmd.CommandText = strQuery;
            cmd.ExecuteNonQuery();
        }
    }
}
catch(Exception ex)
{
    //Handle your exception   
}

捕获异常并检查数据库或查询字符串中的问题。

使用 try-catch 块来了解您得到的异常。

删除 sql 数据适配器,因为您没有使用它。

尝试参考这个 link

exec sp_executesql N'UPDATE Users SET Image=@data WHERE Username=@user',N'@data image,@user nvarchar(35)',@data=default,@user=N'System.Web.UI.WebControls.LoginName' - Found this in profiler

假设 LoginName1 是一个 TextBox,您必须访问 Text 属性 来获取值。如果你 ToString() 一个控件你得到你所看到的,控件的名称:System.Web.UI.WebControls.LoginName

你必须做到:

cmd.Parameters.Add(new SqlParameter("@user", LoginName1.Text));

UPDATE:

我应该注意到 LoginName1 不是 TextBox。您可能必须访问 User 属性,我没有在 LoginName 控件上看到 属性 来获取值,但也许我只是想念它。