是的,我基本上总结出这是某个地方的限制,所以我所做的是创建一个 COM 对象来卸载这项工作并返回我可以在我的脚本中使用的结果数据的 JSON 或 XML 版本。
对于任何关心的人,以下是 C# 代码(如果它有点糟糕,我深表歉意,因为我现在是唯一的开发人员,所以我没有人可以进行代码审查了):
using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Data.SqlClient;
using System.Xml;
/*
* To create strong name key for signing: "C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin\sn" -k SQLInterchange_Key.snk"
*/
namespace SQLInterchange
{
[Guid( "4E97C259-80EC-40dc-8F7D-DB56BE9F123E" )]
public interface ISQLInterchange
{
[DispId( 1 )]
bool Open( string databaseServer, string databaseName, string userID, string userPassword );
[DispId( 2 )]
void Close();
[DispId( 3 )]
bool ExecuteRecordset( string selCommand );
[DispId( 4 )]
void CloseRecordset();
[DispId( 5 )]
bool Execute( string selCommand );
[DispId( 6 )]
string GetJSONData();
[DispId( 7 )]
string GetXMLData( string recordElementName, bool encoded );
}
// Events interface Database_COMObjectEvents
[Guid( "31A125AA-81D5-495b-86E6-7A4B24B08BAA" ),
InterfaceType( ComInterfaceType.InterfaceIsIDispatch )]
public interface SQLInterchange_Events
{
}
[Guid( "6B0B6A04-3BAF-4e14-9770-A0C10425E2CE" ),
ClassInterface(ClassInterfaceType.None),
ComSourceInterfaces(typeof(SQLInterchange_Events))]
public class Connection : ISQLInterchange
{
private SqlConnection _connection = null;
private SqlDataReader _reader = null;
public Connection()
{
}
public bool Open( string databaseServer, string databaseName, string userID, string userPassword )
{
// no need to throw as it throws a com compatible exception automatically
string myConnectString = "user id=" + userID + ";password=" + userPassword +";Database=" + databaseName + ";Server=" + databaseServer + ";Connect Timeout=30";
_connection = new SqlConnection( myConnectString );
_connection.Open();
return true;
}
public bool ExecuteRecordset( string selCommand )
{
if( _reader != null )
_reader.Close();
SqlCommand myCommand = new SqlCommand( selCommand );
myCommand.Connection = _connection;
myCommand.CommandTimeout = 3600;
myCommand.ExecuteNonQuery();
_reader = myCommand.ExecuteReader();
return true;
}
public bool Execute( string selCommand )
{
if( _reader != null )
_reader.Close();
SqlCommand myCommand = new SqlCommand( selCommand, _connection );
myCommand.CommandTimeout = 3600;
int rows = myCommand.ExecuteNonQuery();
return true;
}
public void Close()
{
if( _connection != null )
_connection.Close();
}
public void CloseRecordset()
{
if( _reader != null )
_reader.Close();
}
public string GetJSONData()
{
StringBuilder sb = new StringBuilder();
sb.Append( "[" );
if( _reader != null )
{
int count = _reader.FieldCount;
StringBuilder sbRecord = new StringBuilder();
while( _reader.Read() )
{
if( sbRecord.Length > 0 )
{
sbRecord.Append( "," );
}
sbRecord.Append( "{" );
// get the results of each column
for( int n = 0; n < count; n++ )
{
string name = _reader.GetName( n );
string data = Convert.ToString( _reader[ n ] );
sbRecord.Append( "\"" + _safeJSONElementName( name ) + "\":\"" );
sbRecord.Append( _safeJSON( data ) );
sbRecord.Append( "\"" );
if( n + 1 < count )
{
sbRecord.Append( "," );
}
}
sbRecord.Append( "}" );
}
sb.Append( sbRecord.ToString() );
}
sb.Append( "]" );
return sb.ToString();
}
public string GetXMLData( string recordElementName, bool encoded )
{
_lt = "<";
_gt = ">";
if( encoded )
{
_lt = "<";
_gt = ">";
}
StringBuilder sb = new StringBuilder();
if( _reader != null )
{
int count = _reader.FieldCount;
while( _reader.Read() )
{
_addXMLElement( sb, recordElementName, 1, true );
// get the results of each column
for( int n = 0; n < count; n++ )
{
string name = _reader.GetName( n );
string data = Convert.ToString( _reader[ n ] );
_addXMLElement( sb, name, 2, false );
sb.Append( _escapeXML( data ) );
_addXMLElement( sb, "/" + name, 0, true );
}
_addXMLElement( sb, "/" + recordElementName, 1, true );
}
}
return sb.ToString();
}
private string _safeJSON( string s )
{
s = s.Replace( "\n", "\\n" );
s = s.Replace( "\r", "\\r" );
s = s.Replace( "\t", "\\t" );
s = s.Replace( "\"", "\\\"" );
return s;
}
private string _safeJSONElementName( string s )
{
s = s.Replace( ".", "_" );
s = s.Replace( " ", "_" );
return s;
}
private string _lt = "<";
private string _gt = ">";
private void _addXMLElement( StringBuilder sb, string s, int tabs, bool last )
{
for( int n = 0; n < tabs; n++ )
{
sb.Append( "\t" );
}
sb.Append( _lt );
sb.Append( s );
sb.Append( _gt );
if( last ) sb.Append( "\n" );
}
private string _escapeXML( string unescaped )
{
XmlDocument doc = new XmlDocument();
var node = doc.CreateElement("root");
node.InnerText = unescaped;
return node.InnerXml;
}
}
}