、、、
/// <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 [email protected] 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,content.Length); stream.Close(); InsertCommand.Parameters["@Content"].Value = content; //为参数赋值 try { conn.open(); InsertCommand.ExecuteNonQuery(); } finally { conn.Close(); } } }
---
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。