C# 檔案與二進位互轉資料庫寫入讀出

來源:互聯網
上載者:User

轉自:http://blog.csdn.net/fairyeye/article/details/6970266

 //這個方法是瀏覽檔案對象        private void button1_Click(object sender, EventArgs e)        {            //使用者開啟檔案瀏覽            using (OpenFileDialog dialog = new OpenFileDialog())            {                //只能單選一個檔案                dialog.Multiselect = false;                //選擇一個檔案                if (dialog.ShowDialog() == DialogResult.OK)                {                    try                    {                        //把選擇的檔案路徑給txtPath                        this.textBox1.Text = dialog.FileName;                    }                    catch (Exception ex)                    {                        //拋出異常                        throw (ex);                    }                }            }        }        //關閉        private void button3_Click(object sender, EventArgs e)        {            this.Close();        }        //把檔案轉成二進位流出入資料庫        private void button2_Click(object sender, EventArgs e)        {            FileStream fs = new FileStream(textBox1.Text, FileMode.Open);            BinaryReader br = new BinaryReader(fs);            Byte[] byData = br.ReadBytes((int)fs.Length);            fs.Close();            string conn = "server=.;database=testDB;Uid=sa;Pwd=sa ";            SqlConnection myconn = new SqlConnection(conn);            myconn.Open();            string str = "insert into pro_table (pro_name,pro_file) values('測試檔案',@file)";            SqlCommand mycomm = new SqlCommand(str, myconn);            mycomm.Parameters.Add("@file", SqlDbType.Binary, byData.Length);            mycomm.Parameters["@file"].Value = byData;            mycomm.ExecuteNonQuery();            myconn.Close();        }        //從資料庫中把二進位流讀出寫入還原成檔案        private void button4_Click(object sender, EventArgs e)        {            string conn = "server=.;database=testDB;Uid=sa;Pwd=sa ";            string str = "select pro_file from pro_table where pro_name='測試檔案' ";            SqlConnection myconn = new SqlConnection(conn);            SqlDataAdapter sda = new SqlDataAdapter(str, conn);            DataSet myds = new DataSet();            myconn.Open();            sda.Fill(myds);            myconn.Close();            Byte[] Files = (Byte[])myds.Tables[0].Rows[0]["pro_file"];             BinaryWriter bw = new BinaryWriter(File.Open("D:\\2.rdlc",FileMode.OpenOrCreate));            bw.Write(Files);            bw.Close();                      }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.