sqlcmd.ExecuteNonQuery();
}
}
}
bytContent = null;
fs.Close();
}
catch
{
state = 0;
bytContent = null;
fs.Close();
return false;
}
state = 0;
return true;
}
/// <summary>
/// 从数据库读取word
/// </summary>
/// <param name="sqlcon">sql连接类</param>
/// <param name="strTargetSelect">直接获取目标内容的sql,"select 目标列名 from 目标表名 where 条件"</param>
/// <param name="strTargetLength">获取目标内容的长度的sql,"select datalength(目标列名) from 目标表名 where 条件"</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 ReadWordDocument(System.Data.SqlClient.SqlConnection sqlcon,string strTargetSelect,string strTargetLength,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; //获取的p_w_picpath列中的内容的长度
string strSelect = ""; //要执行的sql查询语句
byte []bytContent = null; //定义内容数组
int intPercent = 0; //获取读取的比例
//建立要输出的文件流
System.IO.FileStream fs = new FileStream(strPath,System.IO.FileMode.Create);
try
{
//获取指定p_w_picpath列中的内容长度
sqlcmd.CommandText = strTargetLength;
intLength = (int)sqlcmd.ExecuteScalar();
//如果长度为0
if(intLength == 0)
{
return false;
}
//获得分快数
intCount = intLength / intBlock;
//是否显示精度
if(bolShowDialog)
{
//获取精度窗体,引用窗体中的进度条和按钮控件
System.Windows.Forms.Form frmProgress = getDialog(strFormName,ico,strShowContent);
System.Windows.Forms.ProgressBar prgLoader = null;
System.Windows.Forms.Button btnOk = null;
System.Windows.Forms.Label lblShowPercent = null;
foreach(System.Windows.Forms.Control control in frmProgress.Controls)
{
if(control.GetType().ToString() == "System.Windows.Forms.ProgressBar")
{
prgLoader = (System.Windows.Forms.ProgressBar)control;
}
if(control.GetType().ToString() == "System.Windows.Forms.Button")
{
btnOk = (System.Windows.Forms.Button)control;
}
if(control.GetType().ToString() == "System.Windows.Forms.Label" && control.Name == "lblShowPercent")
{
lblShowPercent = (System.Windows.Forms.Label)control;
}
}
frmProgress.Show();
//启动转换
state = 1;
if(intCount == 0)
{
strSelect = strTargetSelect;
sqlcmd.CommandText = strSelect;
bytContent = new byte[intLength];
bytContent = sqlcmd.ExecuteScalar() as byte[];
fs.Write(bytContent,0,intLength);
prgLoader.Value = 100;
lblShowPercent.Text = prgLoader.Value + "%";
btnOk.Text = "关闭";
}
else
{
int i = 0;
bytContent = new byte[intBlock];
while(i < intCount)
{
if(state == 0)
{
bytContent = null;
System.IO.File.Delete(strPath);
fs.Close();
return false;
}
strSelect = "declare @content varbinary(16) "; //再sql中声明获取目标p_w_picpath列内容的句柄变量
strSelect += strTargetHandle; //获取句柄
//锁定并读取指定长度的数据
strSelect += " readtext " + strTableName + "." + strColumnName + " @content @start @count HOLDLOCK";
if(i == 0)
{
//添加@start和@count变量,分别表偏移变量和取的长度
sqlcmd.Parameters.Add("@start",System.Data.SqlDbType.Int).Value = 0;
sqlcmd.Parameters.Add("@count",System.Data.SqlDbType.Int).Value = intBlock;
}
else
{
sqlcmd.Parameters["@start"].Value = i * intBlock;
sqlcmd.Parameters["@count"].Value = intBlock;
}
sqlcmd.CommandText = strSelect;
bytContent = sqlcmd.ExecuteScalar() as byte[];
fs.Write(bytContent,0,intBlock);
intPercent = (int)(((double)(i * intBlock)) / (double)(intLength) * 100);
prgLoader.Value = intPercent;
lblShowPercent.Text = prgLoader.Value.ToString() + "%";
Application.DoEvents();
++i;
}
//将剩余的字节写入流
int intResidual = intLength % intBlock;
if(intResidual > 0)
{
strSelect = "declare @content varbinary(16) "; //再sql中声明获取目标p_w_picpath列内容的句柄变量
strSelect += strTargetHandle; //获取句柄
//锁定并读取指定长度的数据
strSelect += " readtext " + strTableName + "." + strColumnName + " @content @start @count HOLDLOCK";
bytContent = new byte[intResidual];
sqlcmd.Parameters["@start"].Value = intCount * intBlock;
sqlcmd.Parameters["@count"].Value = intResidual;
sqlcmd.CommandText = strSelect;
bytContent = sqlcmd.ExecuteScalar() as byte[];
fs.Write(bytContent,0,intResidual);