关于 Sql Server调用 .NET生成的dll所引发的问题
问题陈述如下:
软件环境:
VS.net 2003
Sql Server 2000
Windows Server 2003
步骤1.在vs.net里建立了一个类库 名称为XmlConvertor代码如下:
using System;
using System.Xml;
using System.Xml.Xsl;
using System.Xml.XPath;
using System.Collections;
using System.IO;
using System.Net;
namespace XmlConvertor
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
public class XmlConvertor
{
private XmlDocument XD;
private XslTransform XT;
public XmlConvertor()
{
XD=new XmlDocument();
XT=new XslTransform();
//
// TODO: 在此处添加构造函数逻辑
//
}
public bool LoadXmlFile(string FileName)
{
XD.Load(FileName);
return (XD==null);
}
public string LoadXSLT(string FileName)
{
XPathDocument XPD=new XPathDocument(FileName);
//XmlUrlResolver resolver = new XmlUrlResolver();
//resolver.Credentials = CredentialCache.DefaultCredentials;
//try
//{
XT.Load(XPD.CreateNavigator(),null,null);
return "done";
//}
//catch(Exception e)
//{
// return e.Message.Replace("/n","");
//}
}
public void WriteHTML(string FileName)
{
MemoryStream t=new MemoryStream();
//try
//{
XT.Transform(XD,null,t,null);
string resultString = System.Text.UTF8Encoding.UTF8.GetString(t.ToArray());
if(!File.Exists(FileName))
{
FileStream fs=File.Create(FileName);
fs.Close();
StreamWriter sw=new StreamWriter(FileName,true,System.Text.Encoding.UTF8);
sw.Write(resultString);
sw.Close();
}
else
{
StreamWriter sw=new StreamWriter(FileName,false,System.Text.Encoding.UTF8);
sw.Write(resultString);
sw.Close();
}
//}
//catch
//{
//}
}
public string XMLDocument
{
get
{
return XD.OwnerDocument.ToString();
}
}
public string XSLTDocument
{
get
{
return XT.ToString();
}
}
}
}
编译后产生XmlConvertor.dll 然后按照帖子http://community.csdn.net/Expert/topic/3947/3947161.xml?temp=.3872949的做法执行
步骤二.dll部署完了,以后,我新建了一个空数据库Test建了一存储过程
CREATE PROCEDURE ConvertXML AS
DECLARE @object int
DECLARE @hr int
DECLARE @outinfo varchar(255)
DECLARE @source varchar(255)
DECLARE @property varchar(255)
EXEC @hr = sp_OACreate 'XmlConvertor.XmlConvertor', @object OUT
EXEC @hr = sp_OAMethod @object, 'LoadXmlFile',@property out,'C:\\Inetpub\\wwwroot\\tempxml\\test.xml'
IF @hr <> 0
BEGIN
EXEC sp_OAGetErrorInfo @object
RETURN
END
EXEC @hr = sp_OAMethod @object, 'LoadXSLT',@property out,'C:\\Inetpub\\wwwroot\\tempxml\\1.xslt'
IF @hr <> 0
BEGIN
EXEC sp_OAGetErrorInfo @object
RETURN
END
EXEC @hr = sp_OAMethod @object, 'WriteHTML',NULL,'C:\\Inetpub\\wwwroot\\tempxml\\1.html'
IF @hr <> 0
BEGIN
EXEC sp_OAGetErrorInfo @object
RETURN
END
EXEC @hr = sp_OADestroy @object
GO
步骤三 调试
用查询分析器调试,总是在LoadXSLT处出错,我一度怀疑是组件的问题,但是当我在别的工程里引用同位置下的dll并测试就可以通过。另外比较纳闷的是,如果我把1.xslt的内容删减到最精简的方式:
<?xml version='1.0' encoding='UTF-8'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
</xsl:stylesheet>
则可以在查询分析器里通过,即使形如
<?xml version='1.0' encoding='UTF-8'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
Test
</xsl:template>
</xsl:stylesheet>
的xslt也通不过。
请大家指教。