zoukankan      html  css  js  c++  java
  • C# 文件在数据库 的 存取

    、、、

            /// <summary>
            /// 获取数据库Image字段数据,保存到本地
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void button2_Click(object sender, EventArgs e)
            {
                string constr = "Data Source=IP地址服务器地址;Initial Catalog=SunS;User ID=sa;Password=1321654";
                SqlConnection con = new SqlConnection(constr);
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = "Select * from FileServer With (NoLock) Where FileName = '" + textBox1.Text + "'";
                cmd.CommandType = CommandType.Text;
                cmd.Connection = con;
                con.Open();
                DataTable dt = new DataTable();
                SqlDataAdapter ad = new SqlDataAdapter(cmd);// cmd.EndExecuteReader();
                ad.Fill(dt);
                con.Close();
                cmd.Dispose();
    
                byte[] pics1 = (byte[])dt.Rows[0]["CONTENT"];// 的值强制转换;
                writefile(pics1, "D://" + textBox1.Text + "");
            }
    
            public void writefile(byte[] pics, string filename)
            {
                FileStream fs = new FileStream(filename, FileMode.Append, FileAccess.Write);
                BinaryWriter bw = new BinaryWriter(fs);
                bw.Write(pics, 0, pics.Length);
                bw.Close();
                fs.Close();
            }
            /// <summary>
            /// 将文件转换成byte数组,写入数据库
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void button1_Click(object sender, EventArgs e)
            {
                FileInfo finfo = new FileInfo("D://" + textBox1.Text); //绝对路径
                if (finfo.Exists)
                {
                    SqlConnection conn = new SqlConnection( "Data Source=IP地址服务器地址;Initial Catalog=Sun;User ID=sa;Password=1312313");
                    SqlCommand InsertCommand = new SqlCommand();
                    InsertCommand.Connection = conn;
                    InsertCommand.CommandText = " UPDATE  FileServer SET CONTENT =@Content  WHERE [FILENAME] = '" + textBox1.Text + "'";
                    InsertCommand.Parameters.Add("@Content", SqlDbType.Image, (int)finfo.Length, "CONTENT"); //注意,此处参数Size为写入的字节数
                    //读取文件内容,写入byte数组
                    byte[] content = new byte[finfo.Length];
                    FileStream stream = finfo.OpenRead();
                    stream.Read(content, 0, content.Length);
                    stream.Close();
                    InsertCommand.Parameters["@Content"].Value = content; //为参数赋值
                    try
                    {
                        conn.Open();
                        InsertCommand.ExecuteNonQuery();
                    }
                    finally
                    {
                        conn.Close();
                    }
                }
            }

    ---

  • 相关阅读:
    HTML当中特殊字符的表示
    溢出文本用“...”代替
    【转】图标字体化浅谈
    字体在网页中画ICON图标
    图片轮播
    js获取网页屏幕可视区域高度
    MVC入口程序 | 简单调用及实例化
    初学者对于MVC架构模式学习与理解
    PHP初学习笔记(2015/4/8)
    linux常用20命令 --转载
  • 原文地址:https://www.cnblogs.com/lanyubaicl/p/11162948.html
Copyright © 2011-2022 走看看