/// <summary> /// Add Ph to Album /// </summary> /// <param name="alId">Id of Album to add Ph to</param> /// <param name="Ph">Ph to add</param> public static void AddPh(int alId, Ph Ph) { using (SqlConnection conn = new SqlConnection(ConnectionString)) { using (SqlCommand cmd = new SqlCommand("InsertPh", conn)) { cmd.CommandType = System.Data.CommandType.StoredProcedure; conn.Open(); // Add the name parameter and set the value cmd.Parameters.AddWithValue("@name", Ph.Name); // Add the description parameter and set the value cmd.Parameters.AddWithValue("@desc", Ph.Description); // Add the image parameter and set the value cmd.Parameters.AddWithValue("@Ph", Ph.Image); // Add the album parameter and set the value cmd.Parameters.AddWithValue("@albumId", alId); // Add the return value parameter SqlParameter param = cmd.Parameters.Add("RETURN_VALUE", SqlDbType.Int); param.Direction = ParameterDirection.ReturnValue; // Execute the insert cmd.ExecuteNonQuery(); // Return value will be the index of the newly added Ph Ph.Id = cmd.Parameters["RETURN_VALUE"].Value.ToString(); } } }
/// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void OnAddPh(object sender, EventArgs e) { if (DialogResult.OK == openFileDialog1.ShowDialog()) { // Retrieve the Album to add Ph(s) to Al album = (Al)treeAlbums.SelectedNode.Tag; // We allow multiple selections so loop through each one foreach (string file in openFileDialog1.FileNames) { // Create a new stream to load this Ph into System.IO.FileStream stream = new System.IO.FileStream(file, System.IO.FileMode.Open, System.IO.FileAccess.Read); // Create a buffer to hold the stream bytes byte[] buffer = new byte[stream.Length]; // Read the bytes from this stream stream.Read(buffer, 0, (int)stream.Length); // Now we can close the stream stream.Close(); Ph Ph = new Ph() { // Extract out the name of the file an use it for the name // of the Ph Name = System.IO.Path.GetFileNameWithoutExtension(file), Image = buffer }; // Insert the image into the database and add it to the tree Data.AddPh(album.Id, Ph); buffer = null; // Add the Ph to the album node TreeNode node = treeAlbums.SelectedNode.Nodes.Add(Ph.Name); node.Tag = Ph; } } }