2016-05-07 2 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タグを追加し、srcからdbから取得し、タグを設定する必要があります。 – Mehrdad

+0

どのような画像ですか?あなたが提供したコードには、画像を扱うものは何もありません。 – Patrick

答えて

0

格納するはずのイメージデータ(バイストリーム)が重いため、画像をリレーショナルデータベースに保存したくないです。

サーバー上にリポジトリを作成し、そこにイメージを保存することができます(ユーザーがアクセスするための適切なアクセス許可を設定するのを忘れないでください)。イメージを保存する代わりに製品を作成する間、場所の詳細を保存します。

製品の詳細を読み込んだら、データベースからその場所を取得します。 : - ASP.NET handlerを作成して、クライアントブラウザでイメージをロードします。ハンドラでは、リポジトリからイメージを要求し、イメージデータからBITMAPを作成します。 IMGタグを追加して、クエリ文字列としてハンドラ/ ImageHandler.ashxproductId = {{[商品}} & imageNo = {{NO}}ようなソースを与えるブラウザで画像を表示する

。これにより画像が表示されます。

新しい画像を置き換え、既存の画像を置き換えてFileUploadを使用して画像をアップロードし、既存の画像を新しい画像と置き換えます。したがって、データベースを更新する必要はありません(ただし、追跡および更新するためにユーザー監査を行うことを好みます)。

関連する問題