国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#實現文件與二進制互轉并存入數據庫

2020-01-24 01:41:02
字體:
來源:轉載
供稿:網友
//這個方法是瀏覽文件對象    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();           }
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 潞城市| 昔阳县| 广丰县| 西乌珠穆沁旗| 德安县| 礼泉县| 北宁市| 江达县| 辽阳县| 四会市| 康定县| 西平县| 丹东市| 石城县| 天津市| 普格县| 芜湖县| 临漳县| 汝州市| 新乐市| 沾益县| 泗阳县| 五家渠市| 双牌县| 射阳县| 兰州市| 拜泉县| 石首市| 乌恰县| 武川县| 东丰县| 兰溪市| 溧阳市| 景谷| 绥德县| 洛隆县| 东安县| 灵璧县| 汝阳县| 北碚区| 六盘水市|