日期:2014-05-16  浏览次数:20510 次

二进制存入读取数据库

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);