二进制存入读取数据库

来源:互联网 发布:teamviewer12 for mac 编辑:程序博客网 时间:2024/05/16 11:59

SQl数据脚本

use test
go
create table Categories

(
CategoryID int primary key identity(1,1),
CategoryName varchar(50),

Picture image
)
go

后台代码:

 SqlDataAdapter sda = null;
        DataTable dt = null;

        public void bind()
        {
            SqlConnection conn = new SqlConnection("server=8KQCDK6WIOTOHDM\\SQLEXPRESS;integrated security=True;database=test");
            conn.Open();

            sda = new SqlDataAdapter("select *from Categories ", conn);

            dt = new DataTable();

            sda.Fill(dt);

            this.bindingSource1.DataSource = dt;

 

            dataGridView1.DataSource = bindingSource1;

            this.bindingNavigator1.BindingSource = bindingSource1;
        }


        private void toolStripButton1_Click(object sender, EventArgs e)
        {

            SqlCommandBuilder build = new SqlCommandBuilder(sda);

            sda.DeleteCommand = build.GetDeleteCommand();
            sda.UpdateCommand = build.GetUpdateCommand();
            sda.InsertCommand = build.GetInsertCommand();

            sda.Update(dt);
        }


        //浏览
        private void btsearch_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "图片文件(.jpg)|*.jpg|文本文件|*.txt|所有文件|*.*";
            openFileDialog1.FilterIndex = 1;
            DialogResult dr = openFileDialog1.ShowDialog();

            if (dr == DialogResult.OK)//Yes No Cancel
            {

                string str = openFileDialog1.FileName;

                this.txtpic.Text = str;
            }
        }

        //上传
        private void btok_Click(object sender, EventArgs e)
        {
            if (this.txtpic.Text.Trim() != "")
            {
                FileStream fs = new FileStream(this.txtpic.Text.Trim(), FileMode.Open);
                BinaryReader br = new BinaryReader(fs);
                byte[] bt = new byte[fs.Length];
                br.Read(bt, 0, (int)fs.Length);
                fs.Close();


                SqlConnection conn = new SqlConnection("server=8KQCDK6WIOTOHDM\\SQLEXPRESS;integrated security=True;database=test");

                conn.Open();

                SqlCommand cmd = new SqlCommand("insert into Categories(CategoryName,Picture) values('" + this.txtname.Text.Trim() + "',@pic)", conn);

                SqlParameter param = cmd.Parameters.Add("@pic", SqlDbType.Image);

                param.Value = bt;

                int i = cmd.ExecuteNonQuery();

                if (i >= 1) { MessageBox.Show("上传成功");
                bind();
               
                }
                else { MessageBox.Show("上传失败"); }


                conn.Close();
            }
        }
        //保存
        private void btsavse_Click(object sender, EventArgs e)
        {
            saveFileDialog1.Filter = "图片文件(.jpg)|*.jpg|文本文件|*.txt|所有文件|*.*";
            DialogResult dr = saveFileDialog1.ShowDialog();
            if (dr == DialogResult.OK)
            {

                string fileName = saveFileDialog1.FileName;

                //FileIo
                StreamWriter sw = new StreamWriter(fileName, true);

                sw.Write(this.txtname.Text.Trim());

                sw.Flush();

                sw.Close();
            }
        }

        private void demo2_Load(object sender, EventArgs e)
        {bind();
       
        }
    }
}
       

界面

 

 

0 0