Ø ** 在 .NET ** ** 中利用 FORMULA ** ** ONE ** ** 实现动态报表技术 ( ** ** 二 ) **
4. 读入的模板用二进制流的形式存入数据库中, F1 提供一个 WriteToBlobEx(), 和 WriteToBlob() 方法 , 帮助文档中定义的语法为:
phBlob = F1Book1.WriteToBlobEx (nReservedBytes)
本来可以很轻松的把模板文件写入二进制流,但转换了 .NET 可调用的组件后定义的语法成了:
public virtual new **_ System.Int32 _ ** WriteToBlobEx ( **_ System.Int16 _ ** nReservedBytes )
phBlob 类型成了 System.Int32 类型!因此我们只能转用另外的方法。首先把当前模板保存也一临时文件
// 把当前模板保存到临时文件
axF1Book1.WriteEx("F1Temp.VTS",TTF160.F1FileTypeConstants.F1FileFormulaOne6);
再利用 .NET 函数转换成二进制流 , 之后再删除临时文件 .
// 取得临时文件二进制流
byte [] Data = FileToByte("F1Temp.VTS");
// 删除临时文件
File.Delete("F1Temp.VTS");
// 文件-- > Byte[]
private byte [] FileToByte( string FileName)
{
byte [] FileData;
try
{
using (FileStream fs = File.OpenRead(FileName))
{
FileData = new byte [fs.Length];
fs.Read(FileData,0,( int )fs.Length);
}
}
catch (Exception e)
{
MessageBox.Show(" 把文件生成二进制流错误!原因: "+e.Message);
return null ;
}
return FileData;
}
取得二进制流后,判断数据库中是不是已经有此模板,如果已经有此模板则对其进行更新,否则进行新增操作。
新增操作代码如下:
#region 插入当前模板到数据库
strSQL = "Insert Into Report_List_Data(num,rpt_id,rpt_name,user_name,rpt_date,rpt_data)";
strSQL += " Values(@PKID,@rptID,@rptName,@rptUserName,GetDate(),@rptData)";
SqlCommand command = new SqlCommand(strSQL,funcLib.aConn);
// 插入参数
SqlParameter paramPKID = new SqlParameter("@PKID",SqlDbType.Int);
paramPKID.Value = GetPKID("Report_List_Data","num");
command.Parameters.Add( paramPKID );
SqlParameter paramRptID = new SqlParameter("@rptID",SqlDbType.Int);
paramRptID.Value = tplInfo[1];
command.Parameters.Add(paramRptID);
SqlParameter paramRptName = new SqlParameter("@rptName",SqlDbType.NVarChar);
paramRptName.Value = tplInfo[0];
command.Parameters.Add(paramRptName);
SqlParameter paramRptUserName = new SqlParameter("@rptUserName",SqlDbType.NVarChar);
paramRptUserName.Value = "HY";
command.Parameters.Add(paramRptUserName);
SqlParameter paramRptData = new SqlParameter("@rptData",SqlDbType.Image);
paramRptData.Value = Data;
command.Parameters.Add(paramRptData);
if (funcLib.aConn.State!=ConnectionState.Open) funcLib.aConn.Open();
command.ExecuteNonQuery();
}
catch (Exception err)
{
MessageBox.Show(" 插入当前模板不成功!原因: "+err.Message);
return ;
}
#endregion
对模板进行更新操作代码如下所示:
#region 把修改后的模板保存到数据库
try
{
strSQL = "Update Report_List_Data Set rpt_data = @rptData where rpt_id = "+tplInfo[1] + " and rpt_name = '"+tplInfo[0]+"'";
SqlCommand command = new SqlCommand(strSQL,funcLib.aConn);
// 插入参数
SqlParameter paramRptData = new SqlParameter("@rptData",SqlDbType.Image);
paramRptData.Value = Data;
command.Parameters.Add( paramRptData );
if (funcLib.aConn.State!=ConnectionState.Open) funcLib.aConn.Open();
command.ExecuteNonQuery();
break ;
}
catch (Exception err)
{
MessageBox.Show(" 保存当前模板不成功!原因: "+err.Message," 错误 ");
return ;
}
#endregion
注:其中 funcLib 为对数据库操作的基类 funcLib.aConn 为取得当前连接的实例, GetPKID("Report_List_Data","num") 函数功能是取得指定表名和其主键字段名的最大主键值。