`

C#保存文件或读取数据库文件 另存为

    博客分类:
  • C#
 
阅读更多

/// <summary>
        
/// 保存文件到数据库
        
/// </summary>
        
/// <param name="sender"></param>
        
/// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            
string conn = @"Data Source=YANGYIBANG\SQLEXPRESS;Initial Catalog=joe;Integrated Security=True";
            SqlConnection cn 
= new SqlConnection(conn);
            Stream myStream 
= null;
            OpenFileDialog openFileDialog1 
= new OpenFileDialog();

            
//openFileDialog1.InitialDirectory = "c:\\";
            
//openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
            
//openFileDialog1.FilterIndex = 2;
            openFileDialog1.RestoreDirectory = true;

            
if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                
try
                {
                    FileInfo fi 
= new FileInfo(openFileDialog1.FileName);
                    FileStream fs 
= fi.OpenRead();
                    
byte[] bytes = new byte[fs.Length];
                    fs.Read(bytes, 
0, Convert.ToInt32(fs.Length));
                    SqlCommand cm 
= new SqlCommand();
                    cm.Connection 
= cn;
                    cm.CommandType 
= CommandType.Text;
                    
if (cn.State == 0) cn.Open();
                    cm.CommandText 
= "insert into title (titileName, files ) values('aaa',@file)";
                    SqlParameter spFile 
= new SqlParameter("@file", SqlDbType.Image);
                    spFile.Value 
= bytes;
                    cm.Parameters.Add(spFile);
                    cm.ExecuteNonQuery();
                }
                
catch (Exception ex)
                {
                    MessageBox.Show(
"Error: Could not read file from disk. Original error: " + ex.Message);
                }
            }

            

        }


        
/// <summary>
        
/// 从数据库就读取文件另存为
        
/// </summary>
        
/// <param name="sender"></param>
        
/// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            
try
            {

            
            
string conn = @"Data Source=YANGYIBANG\SQLEXPRESS;Initial Catalog=joe;Integrated Security=True";
            SqlConnection cn 
= new SqlConnection(conn);
            SqlDataReader dr 
= null;
            SqlCommand cm 
= new SqlCommand();
            cm.Connection 
= cn;
            cm.CommandType 
= CommandType.Text;
            cm.CommandText 
= "select  files  from title where titileId=5";
            cn.Open();
            dr 
= cm.ExecuteReader();
            
byte[] File = null;
            
if (dr.Read())
            {
                File 
= (byte[])dr[0];
            }

            dr.Close();
            cn.Close();
          
                
/*
                 * 直接另存为文件
                 * 
                 
*/ 
            SaveFileDialog saveFileDialog1 
= new SaveFileDialog();
            Stream myStream;

            
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                
if ((myStream = saveFileDialog1.OpenFile()) != null)
                {
                    
using (BinaryWriter bw = new BinaryWriter(myStream))
                    {
                        
// Add some text to the file.
                        bw.Write(File);
                    }

                }
            }
                
           
            }
            
catch (Exception ex)
            {

                MessageBox.Show(
"Error: Could not read file from db. Original error: " + ex.Message);
            }
        }

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics