简体   繁体   English

如何更新数据库中的图像? 时间:2019-04-10标签:asp.netsql

[英]How do you update an image in a database? c# asp.net sql

This is my code, which all works fine, so I don't want to change the way I'm doing it. 这是我的代码,一切正常,所以我不想更改执行方式。 However I'm at a loss as to what to do with the image. 但是,我对如何处理图像一无所知。 How do I display it for the user to edit, and then update their new upload to the database? 如何显示它供用户编辑,然后将其新上传的内容更新到数据库?

    protected void Page_Load(object sender, EventArgs e)
{

    if (!IsPostBack) { 
    int row = 0;
    if (Request.QueryString["Advertisement"] != null) {

        row = int.Parse(Request.QueryString["Advertisement"]);
    }
    else
    {
        Response.Redirect("ViewAdvertisements.aspx");
    }

    string connectionString = WebConfigurationManager.ConnectionStrings["ElmtreeConnection"].ConnectionString;

    SqlConnection myConnection = new SqlConnection(connectionString);

    myConnection.Open();

    string query = "SELECT * FROM Products WHERE Id=@rowid";

    SqlCommand myCommand = new SqlCommand(query, myConnection);

    myCommand.Parameters.AddWithValue("@rowid", row);

    SqlDataReader rdr = myCommand.ExecuteReader();

    while (rdr.Read())
    {
        editadtitle.Text = (rdr["name"].ToString());
        editdescription.Text = (rdr["description"].ToString());
        editprice.Text = (rdr["price"].ToString());
        editcategory.Text = (rdr["categoryid"].ToString());



    }



}
}

   protected void btnSignOut_Click(object sender, EventArgs e)
{
    Session["SELLER"] = null;
    Response.Redirect("Default.aspx");
}

protected void updatebutton_Click(object sender, EventArgs e)
{




    string connectionString = WebConfigurationManager.ConnectionStrings["ElmtreeConnection"].ConnectionString;

    SqlConnection myConnection = new SqlConnection(connectionString);


    myConnection.Open();

    string adtitleupdate = editadtitle.Text;
    int row = int.Parse(Request.QueryString["Advertisement"]);
    string descriptionupdate = editdescription.Text;
    string priceupdate = editprice.Text;
    string categoryupdate = editcategory.Text;



    string query = "UPDATE Products SET Name = @newadtitle, Description = @newdescription, Price = @newprice, CategoryId = @newcategory WHERE Id = @id";

    SqlCommand myCommand = new SqlCommand(query, myConnection);

    myCommand.Parameters.AddWithValue("@newadtitle", adtitleupdate);
    myCommand.Parameters.AddWithValue("@id",row);
    myCommand.Parameters.AddWithValue("@newdescription", descriptionupdate);
    myCommand.Parameters.AddWithValue("@newprice", priceupdate);
    myCommand.Parameters.AddWithValue("@newcategory", categoryupdate);





    updatelabel.Text = "Your information has now been updated. ";



    myCommand.ExecuteNonQuery();

    myConnection.Close();

}
 }

I prefer not to store the images in relational databases, because the imagedata (bytestreams) which you supposed to store are heavy. 我不希望将图像存储在关系数据库中,因为您应该存储的图像数据(字节流)很重。

You can create a repository on your server and store the images over there (dont forget to set appropriate permission for the user to access). 您可以在服务器上创建一个存储库,并将图像存储在该服务器上(不要忘记为用户访问设置适当的权限)。 While you create a product instead of storing the image save the location details. 在创建产品而不是存储图像时,请保存位置详细信息。

Once you load the product details take the location from the database. 加载产品详细信息后,请从数据库中获取位置。 :- Create a ASP.NET handler to load the images on the client browser. :-创建一个ASP.NET处理程序以在客户端浏览器上加载图像。 In Handler request the images from the repository and create BITMAP from the image data. 在处理程序中,从存储库请求图像,并从图像数据创建BITMAP。

To display the images in browser add an IMG tag and give the source as Handlers/ImageHandler.ashxproductId={{productId}}&imageNo={{No}} as the query string. 要在浏览器中显示图像,请添加IMG标签,并将源指定为Handlers / ImageHandler.ashxproductId = {{productId}}&imageNo = {{No}}作为查询字符串。 This will result to display the images. 这将导致显示图像。

To Upload new image and replace the existing one use the FileUpload to upload the image and replace the existing one with new one. 上传新图像并替换现有图像 ,请使用FileUpload上传图像并将现有图像替换为新图像。 So you dont need to update the database (but prefer to put some user audit to track and update). 因此,您不需要更新数据库(但更喜欢进行一些用户审核来跟踪和更新)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 (C#) 如何将图像文件从 ASP.NET MVC 5 web 表单发送到我的 SQL 数据库? - (C#) How do I send an image file from an ASP.NET MVC 5 web form to my SQL database? 如何使用C#使用SQL Server数据库更新ASP.NET中的配置文件 - How to update profile in ASP.NET with SQL Server database using C# 在ASP.NET C#中的两个列表之间拖放后如何更新sql数据库? - how to update the sql database after the drag and drop between two list in asp.net C#? 如何通过C#ASP.NET MVC4更新数据库? - How to update database by C# ASP.NET MVC4? ASP.Net如何从gridview C#更新数据库 - ASP.Net How to Update a database from a gridview C# 如何在ASP.NET C#中使用if(IsPostBack) - How do you use if (IsPostBack) in ASP.NET C# asp.net mvc 更新数据库中的项目时如何更新数据库(数据库未正确更新)? - asp.net mvc How do you update a database when you update an item in it (database not updating correctly)? 在Asp.net(c#)中,如何在数据库中存储图像(linq to sql)? - In Asp.net (c#) ,how to store an image in database(linq to sql)? 如何在Asp.Net C#中将图像上传到SQL数据库 - How to Upload Images into SQL Database in Asp.Net C# 将ui中的图像与SQL Server数据库Asp.net中保存的图像进行比较,C# - comparing image from ui to saved image in SQL server database Asp.net, c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM