最近一个项目需要将大文件写入和读取到数据库,觉得可能很多人也需要相关得东西,所以就将代码帖出来
protected int state = 0; //表示进度条当前处理的事件类型,1表读取word,2表写入word,3表doc转pdf,4表txt转pdf
private System.Windows.Forms.Form getDialog( string strFormName,System.Drawing.Icon ico, string strShowContent)
{
System.Windows.Forms.Form frm = new Form();
//初始化窗体
frm.Text = strFormName;
frm.Icon = ico;
frm.MaximizeBox = false ;
frm.MinimizeBox = false ;
frm.TopMost = true ;
frm.ShowInTaskbar = false ;
frm.Height = 168;
frm.Width = 544;
frm.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
//添加控件
System.Windows.Forms.Label lblContent = new Label();
lblContent.Text = strShowContent;
lblContent.Left = 30;
lblContent.Top = 20;
lblContent.Text = strShowContent;
frm.Controls.Add(lblContent);
System.Windows.Forms.ProgressBar prgLoader = new ProgressBar();
prgLoader.Left=30;
prgLoader.Top = lblContent.Top + lblContent.Height + 5;
prgLoader.Width = frm.Width - 2 * 30;
frm.Controls.Add(prgLoader);
System.Windows.Forms.Label lblShowPercent = new Label();
lblShowPercent.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
lblShowPercent.Left = prgLoader.Width + 30 - lblShowPercent.Width;
lblShowPercent.Top = prgLoader.Height + prgLoader.Top + 5;
lblShowPercent.Text = prgLoader.Value.ToString() + "%" ;
lblShowPercent.Name = "lblShowPercent" ;
frm.Controls.Add(lblShowPercent);
System.Windows.Forms.Button btnOK = new Button();
btnOK.Text = "取消" ;
btnOK.Left = prgLoader.Width + 30 - btnOK.Width;
btnOK.Top = frm.Height - 30 - btnOK.Height;
btnOK.Click += new EventHandler(btnOk_Click);
frm.Controls.Add(btnOK);
return frm;
}
private void btnOk_Click( object sender,System.EventArgs e)
{
//获取控件信息
System.Windows.Forms.Button btnOk = (System.Windows.Forms.Button)sender;
System.Windows.Forms.Form frm = (System.Windows.Forms.Form)btnOk.Parent;
System.Windows.Forms.ProgressBar prgLoader = null ;
foreach (System.Windows.Forms.Control control in frm.Controls)
{
if (control.GetType().ToString() == "System.Windows.Forms.ProgressBar" )
{
prgLoader = (System.Windows.Forms.ProgressBar)control;
}
}
//判断当前的完成情况
if (prgLoader.Value == 100)
{
frm.Close();
}
else
{
System.Windows.Forms.DialogResult dr = MessageBox.Show(frm, "是否停止当前操作?" , "提示" ,System.Windows.Forms.MessageBoxButtons.YesNo,
System.Windows.Forms.MessageBoxIcon.Warning);
if (dr == System.Windows.Forms.DialogResult.Yes)
{
state = 0;
frm.Close();
}
}
}
/// <summary>
/// 写入word到数据库
/// </summary>
/// <param name="sqlcon">sql连接类</param>
/// <param name="strTargetInsert">直接将目标内容写入数据库的sql,"insert into 表名(目标列名) values (@block)"</param>
/// <param name="strTargetHandle">获取目标内容的句柄的sql,"select @content=textptr(目标列名) from 目标表名 where 条件"</param>
/// <param name="strTableName">目标表名</param>
/// <param name="strColumnName">目标列名</param>
/// <param name="strPath">要读取word的路径</param>
/// <param name="intSetBlock">定义块大小</param>
/// <param name="bolShowDialog">是否显示进度条</param>
/// <param name="strFormName">窗体名称</param>
/// <param name="ico">窗体图标</param>
/// <param name="strShowContent">显示内容</param>
/// <returns>true表成功</returns>
public bool WriteWordDocument(System.Data.SqlClient.SqlConnection sqlcon, string strTargetInsert, string strTargetHandle, string strTableName, string strColumnName, string strPath, int intSetBlock, bool bolShowDialog, string strFormName,System.Drawing.Icon ico, string strShowContent)
{
//初始化SqlCommand
System.Data.SqlClient.SqlCommand sqlcmd = new System.Data.SqlClient.SqlCommand();
sqlcmd.Connection = sqlcon;
sqlcmd.CommandType = System.Data.CommandType.Text;
int intBlock = intSetBlock; //块大小
int intCount = 0; //分快数
int intLength = 0; //获取文件内容的长度
string strSelect = ""; //要执行的sql查询语句
byte []bytContent = null ; //定义内容数组
//比例
int intPercent = 0;
//建立要输入的文件流
System.IO.FileStream fs = null ;
//建立二进制读取
System.IO.BinaryReader br = null ;
try
{
fs = new FileStream(strPath,System.IO.FileMode.Open);
}
catch
{
return false ;
}
br = new BinaryReader((Stream)fs);
//为关键参数赋值
try
{
//是否显示进度
if (bolShowDialog)
{
//获取精度窗体,引用窗体中的进度条和按钮控件
System.Windows.Forms.Form frmProgress = getDialog(strFormName,ico,strShowContent);
protected int state = 0; //表示进度条当前处理的事件类型,1表读取word,2表写入word,3表doc转pdf,4表txt转pdf
private System.Windows.Forms.Form getDialog( string strFormName,System.Drawing.Icon ico, string strShowContent)
{
System.Windows.Forms.Form frm = new Form();
//初始化窗体
frm.Text = strFormName;
frm.Icon = ico;
frm.MaximizeBox = false ;
frm.MinimizeBox = false ;
frm.TopMost = true ;
frm.ShowInTaskbar = false ;
frm.Height = 168;
frm.Width = 544;
frm.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
//添加控件
System.Windows.Forms.Label lblContent = new Label();
lblContent.Text = strShowContent;
lblContent.Left = 30;
lblContent.Top = 20;
lblContent.Text = strShowContent;
frm.Controls.Add(lblContent);
System.Windows.Forms.ProgressBar prgLoader = new ProgressBar();
prgLoader.Left=30;
prgLoader.Top = lblContent.Top + lblContent.Height + 5;
prgLoader.Width = frm.Width - 2 * 30;
frm.Controls.Add(prgLoader);
System.Windows.Forms.Label lblShowPercent = new Label();
lblShowPercent.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
lblShowPercent.Left = prgLoader.Width + 30 - lblShowPercent.Width;
lblShowPercent.Top = prgLoader.Height + prgLoader.Top + 5;
lblShowPercent.Text = prgLoader.Value.ToString() + "%" ;
lblShowPercent.Name = "lblShowPercent" ;
frm.Controls.Add(lblShowPercent);
System.Windows.Forms.Button btnOK = new Button();
btnOK.Text = "取消" ;
btnOK.Left = prgLoader.Width + 30 - btnOK.Width;
btnOK.Top = frm.Height - 30 - btnOK.Height;
btnOK.Click += new EventHandler(btnOk_Click);
frm.Controls.Add(btnOK);
return frm;
}
private void btnOk_Click( object sender,System.EventArgs e)
{
//获取控件信息
System.Windows.Forms.Button btnOk = (System.Windows.Forms.Button)sender;
System.Windows.Forms.Form frm = (System.Windows.Forms.Form)btnOk.Parent;
System.Windows.Forms.ProgressBar prgLoader = null ;
foreach (System.Windows.Forms.Control control in frm.Controls)
{
if (control.GetType().ToString() == "System.Windows.Forms.ProgressBar" )
{
prgLoader = (System.Windows.Forms.ProgressBar)control;
}
}
//判断当前的完成情况
if (prgLoader.Value == 100)
{
frm.Close();
}
else
{
System.Windows.Forms.DialogResult dr = MessageBox.Show(frm, "是否停止当前操作?" , "提示" ,System.Windows.Forms.MessageBoxButtons.YesNo,
System.Windows.Forms.MessageBoxIcon.Warning);
if (dr == System.Windows.Forms.DialogResult.Yes)
{
state = 0;
frm.Close();
}
}
}
/// <summary>
/// 写入word到数据库
/// </summary>
/// <param name="sqlcon">sql连接类</param>
/// <param name="strTargetInsert">直接将目标内容写入数据库的sql,"insert into 表名(目标列名) values (@block)"</param>
/// <param name="strTargetHandle">获取目标内容的句柄的sql,"select @content=textptr(目标列名) from 目标表名 where 条件"</param>
/// <param name="strTableName">目标表名</param>
/// <param name="strColumnName">目标列名</param>
/// <param name="strPath">要读取word的路径</param>
/// <param name="intSetBlock">定义块大小</param>
/// <param name="bolShowDialog">是否显示进度条</param>
/// <param name="strFormName">窗体名称</param>
/// <param name="ico">窗体图标</param>
/// <param name="strShowContent">显示内容</param>
/// <returns>true表成功</returns>
public bool WriteWordDocument(System.Data.SqlClient.SqlConnection sqlcon, string strTargetInsert, string strTargetHandle, string strTableName, string strColumnName, string strPath, int intSetBlock, bool bolShowDialog, string strFormName,System.Drawing.Icon ico, string strShowContent)
{
//初始化SqlCommand
System.Data.SqlClient.SqlCommand sqlcmd = new System.Data.SqlClient.SqlCommand();
sqlcmd.Connection = sqlcon;
sqlcmd.CommandType = System.Data.CommandType.Text;
int intBlock = intSetBlock; //块大小
int intCount = 0; //分快数
int intLength = 0; //获取文件内容的长度
string strSelect = ""; //要执行的sql查询语句
byte []bytContent = null ; //定义内容数组
//比例
int intPercent = 0;
//建立要输入的文件流
System.IO.FileStream fs = null ;
//建立二进制读取
System.IO.BinaryReader br = null ;
try
{
fs = new FileStream(strPath,System.IO.FileMode.Open);
}
catch
{
return false ;
}
br = new BinaryReader((Stream)fs);
//为关键参数赋值
try
{
//是否显示进度
if (bolShowDialog)
{
//获取精度窗体,引用窗体中的进度条和按钮控件
System.Windows.Forms.Form frmProgress = getDialog(strFormName,ico,strShowContent);
System.Windows.Forms.ProgressBar prgLoader = null;
本文转自 BruceAndLee 51CTO博客,原文链接:http://blog.51cto.com/leelei/194144,如需转载请自行联系原作者