2016-05-07 39 views
0

這是我的代碼,這一切工作正常,所以我不想改變我這樣做的方式。然而,我不知道該如何處理圖像。如何顯示它供用戶編輯,然後將其新上載更新到數據庫?如何更新數據庫中的圖像? c#asp.net sql

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 [email protected]"; 

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

} 
} 
+0

您應該添加一個img標籤並設置從db中檢索到的src和一個標籤以便選擇一個新標籤。 – Mehrdad

+0

什麼形象?在你提供的代碼中,我沒有看到任何可以處理圖像的代碼。 – Patrick

回答

0

我更喜歡不將圖像存儲在關係數據庫中,因爲您應該存儲的imagedata(bytestreams)很重。

您可以在服務器上創建一個存儲庫,並將圖像存儲在那裏(不要忘記爲用戶設置適當的權限)。創建產品而不是存儲圖像時,請保存位置詳細信息。

加載產品詳細信息後,請從數據庫獲取位置。 : - 創建一個ASP.NET handler加載客戶端瀏覽器上的圖像。在處理程序中請求存儲庫中的圖像並從圖像數據創建BITMAP。

要顯示在瀏覽器中的圖像添加一個IMG標記並給源作爲處理程序/ ImageHandler.ashxproductId = {{的productId}} & imageNo = {{否}}作爲查詢字符串。這將導致顯示圖像。

上傳新圖像並替換現有的使用FileUpload上傳圖像,並用新的圖像替換現有的圖像。所以你不需要更新數據庫(但更喜歡把一些用戶審計來跟蹤和更新)。

相關問題