2016-07-07 1 views
0

上記のコードで画像をTimesSelectで複数選択したパネルをパネルに表示しました。複数のファイルを対象として保存したい場合は、ループを検討なぜこのコードは私が選択している間だけ最初の画像を保存するのですか?1

FileStream fs = new FileStream(files[0], ...); 

:PICは、データベース内の最初の画像は10時間

if (files != null) 
{ 
    SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Avais\Desktop\GUI of GUA\GUI of GUA\GUAdatabase.mdf;Integrated Security=True"); 

    con.Open(); 
    FileStream fs = new FileStream(files[0], System.IO.FileMode.Open, System.IO.FileAccess.Read); 

    byte[] image = new byte[fs.Length]; 
    fs.Read(image, 0, Convert.ToInt32(fs.Length)); 

    fs.Close(); 

    SqlCommand cmd = new SqlCommand("INSERT INTO Admin_Pic_Lib(Pictures) VALUES (@pic)", con); 
    SqlParameter prm = new SqlParameter("@pic", SqlDbType.VarBinary, image.Length, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, image); 

    cmd.Parameters.Add(prm); 

    cmd.ExecuteNonQuery(); 

    label1.ForeColor = Color.Green; 
    label1.Text = "Pic Added Suscussfully"; 

    con.Close(); 
} 

答えて

2

を保存しているあなたは、現在のみ利用可能である第一の画像/ファイルをターゲットにしていますあなたのファイルコレクションを通してこのopeを実行する各自の配給:

foreach(var file in files) 
{ 
     // Read each individual file and save it here 
} 
+0

詳細については、 – Avais

+0

で説明してください。どの部分で問題がありますか?あなたの現在の 'files'コレクションは1つ以上のファイルで構成されていますので、このコレクションを反復し、それぞれを' foreach'ループの本体のデータベースに追加する必要があります。 –

関連する問題