/// <summary> /// 添加技术资料附件 /// </summary> private void btnAddMatDoc_Click(object sender, EventArgs e) { if (this.s_DocName.Text.Trim() == "") { MessageBox.Show("请先填写资料名称!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } //判断是否存在记录 string strSqlSel = ""; if (this.Type == "add") { strSqlSel = "select * from T_MatDocs where DocName='{0}'"; } else { strSqlSel = "select * from T_MatDocs where DocName='{0}' and sysid!=" + this.sysid_; } strSqlSel = string.Format(strSqlSel, this.s_DocName.Text.Trim()); bool isExist = (new DBUtil()).yn_exist_data(strSqlSel); if (isExist) { MessageBox.Show("该资料名已存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } OpenFileDialog fd = new OpenFileDialog(); fd.Filter = "word文档(*.doc)|*.doc|Excel电子表格(*.xls)|*.xls|所有文件(*.*)|*.*"; if (fd.ShowDialog() == DialogResult.OK) { string fileName = fd.FileName; string fileExt = fileName.Substring(fileName.LastIndexOf('.')); //将文件读入到字节数组 System.IO.FileStream fs = new System.IO.FileStream(fd.FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read); byte[] buffByte = new byte[fs.Length]; fs.Read(buffByte, 0, (int)fs.Length); fs.Close(); fs = null; SqlDBConnect dbc = new SqlDBConnect(); string sql_ = "update T_MatDocs set DocBlob=@file,doctype=@doctype where sysid='{0}'"; sql_ = string.Format(sql_, this.sysid_); SqlCommand cm = new SqlCommand(); cm.Connection = dbc.conn; dbc.OpenDb(); cm.CommandText = sql_; cm.Parameters.Add("@file", System.Data.SqlDbType.Image); cm.Parameters.Add("@doctype", System.Data.SqlDbType.NChar); cm.Parameters[0].Value = buffByte; cm.Parameters[1].Value = fileExt; try { cm.ExecuteNonQuery(); MessageBox.Show("上传成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); } catch (Exception ex) { MessageBox.Show("上传失败" + ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); } dbc.CloseDb(); } }
/// <summary> /// 添加附件 /// </summary> private void btnAddDoc_Click(object sender, EventArgs e) { #region 验证 if (this.dataGridView1.SelectedRows.Count <= 0) { return; } if (this.dataGridView1.SelectedRows[0].Cells["sysid"].Value == null) { return; } if (this.dataGridView1.SelectedRows[0].Cells["barsysid"].Value == null) { return; } if (this.s_AttachName.Text.Trim() == "") { MessageBox.Show("请先填写附件名称!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } //判断是否存在记录 string strSqlSel = "select * from T_BargAttach where barsysid={0} and AttachName='{1}'"; strSqlSel = string.Format(strSqlSel, this.barsysid, this.s_AttachName.Text.Trim()); bool isExist = (new DBUtil()).yn_exist_data(strSqlSel); if (isExist == false) { MessageBox.Show("该记录不存在,不能为其添加附件!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } #endregion OpenFileDialog fd = new OpenFileDialog(); fd.Filter = "word文档(*.doc)|*.doc|Excel电子表格(*.xls)|*.xls|所有文件(*.*)|*.*"; if (fd.ShowDialog() == DialogResult.OK) { string fileName = fd.FileName; string fileExt = fileName.Substring(fileName.LastIndexOf('.')); //将文件读入到字节数组 System.IO.FileStream fs = new System.IO.FileStream(fd.FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read); byte[] buffByte = new byte[fs.Length]; fs.Read(buffByte, 0, (int)fs.Length); fs.Close(); fs = null; SqlDBConnect dbc = new SqlDBConnect(); string sql_ = "update T_BargAttach set Attachment=@file,doctype=@doctype where barsysid={0} and AttachName='{1}'"; sql_ = string.Format(sql_, this.barsysid, this.s_AttachName.Text.Trim()); SqlCommand cm = new SqlCommand(); cm.Connection = dbc.conn; dbc.OpenDb(); cm.CommandText = sql_; cm.Parameters.Add("@file", System.Data.SqlDbType.Image); cm.Parameters.Add("@doctype", System.Data.SqlDbType.NChar); cm.Parameters[0].Value = buffByte; cm.Parameters[1].Value = fileExt; try { cm.ExecuteNonQuery(); MessageBox.Show("绑定成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); initdataGridview(); } catch (Exception ex) { MessageBox.Show("绑定失败" + ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); } dbc.CloseDb(); } }