我有同样的任务,看起来你只是无法使用标准 ADODB.Connection 进行连接。我做了一些研究,发现你应该使用 iAnywhere.Data.SQLAnywhere lib 和它自己的 SybaseConnector.Connector 来连接到 Sybase。您可以使用 SybaseConnector 创建自己的 COM 对象,而不是使用批处理来调用 vb vbscript 文件,将其注册到您的系统并创建新对象或生成可执行文件,以获取连接字符串和查询。我可以和你分享源码。
设置 Sybase IQ 15.4 开发版,然后在 Visual Studio 中创建项目并引用 .NET 组件 iAnywhere.Data.SQLAnywhere。
然后使用此代码:
using System;
using System.Collections.Generic;
using System.Text;
using iAnywhere.Data.SQLAnywhere;
using System.Diagnostics;
using System.Data;
namespace SybaseConnector
{
public class Connector
{
private SAConnection _myConnection { get; set; }
private SADataReader _data { get; set; }
private SACommand _comm { get; set; }
private SAConnectionStringBuilder _conStr { get; set; }
public bool isDebug { get; set; }
public Connector(string UserID, string Password, string CommLinks, string ServerName, string command)
{
_conStr = new SAConnectionStringBuilder();
// dynamic
_conStr.UserID = UserID; //"dba";
_conStr.Password = Password; //"sql";
_conStr.CommLinks = CommLinks; //@"TCPIP{IP=servername;ServerPort=2638}";
_conStr.ServerName = ServerName; //"northwind";
// static
_conStr.Compress = "NO";
_conStr.DisableMultiRowFetch = "NO";
_conStr.Encryption = "NONE";
_conStr.Integrated = "NO";
this.Connect();
this.ExecuteCommand(command);
this.WriteResult();
}
public void Connect()
{
_myConnection = new SAConnection();
_myConnection.StateChange += new StateChangeEventHandler(ConnectionControl);
if (_conStr.ToString() != String.Empty)
{
try
{
_myConnection.ConnectionString = _conStr.ToString();
_myConnection.Open();
}
catch(Exception e)
{
if ((int)_myConnection.State == 0)
{
_myConnection.Dispose();
WriteDebug("Exception data:\n" + e.Data + "\n" +
"Exception message:\n" + e.Message + "\n" +
"Inner exception:\n" + e.InnerException + "\n" +
"StackTrace:\n" + e.StackTrace);
}
}
}
}
public void ExecuteCommand(string com, int timeout = 600)
{
if ((int)_myConnection.State != 0)
{
_comm = new SACommand();
_comm.CommandText = com;
_comm.CommandTimeout = timeout;
_comm.Connection = _myConnection;
try
{
_data = _comm.ExecuteReader();
}
catch (Exception e)
{
WriteDebug("Exception data:\n" + e.Data + "\n" +
"Exception message:\n" + e.Message + "\n" +
"Inner exception:\n" + e.InnerException + "\n" +
"StackTrace:\n" + e.StackTrace);
}
}
else
{
WriteDebug("Exception occured:\n" +
"Connection has been closed before the command has been executed.");
}
}
private void ConnectionControl(object sender, StateChangeEventArgs e)
{
WriteDebug(sender.GetType().ToString() + ": Connection state changed to " + e.CurrentState.ToString());
}
public void SetMaxReconnectCount(int count)
{
_reconnectCounter = count;
}
public void Dispose()
{
_comm.Dispose();
_data.Dispose();
_myConnection.Close();
_myConnection.Dispose();
}
private void WriteResult()
{
var output = new StringBuilder();
int count = _data.FieldCount;
// аппенд в строку и вывод в ивент одним объектом
for (int i = 0; i < count; i++)
{
if (i == count - 1)
{
output.Append(_data.GetName(i) + "\n");
}
else
{
output.Append(_data.GetName(i) + "\t");
}
}
while (_data.Read())
{
for (int i = 0; i < count; i++)
{
if (i == count - 1)
{
output.Append(_data.GetValue(i) + "\n");
}
else
{
output.Append(_data.GetValue(i) + "\t");
}
}
}
WriteDebug(output.ToString());
}
private void WriteDebug(string str, EventLogEntryType type = EventLogEntryType.Information)
{
System.Diagnostics.EventLog appLog = new System.Diagnostics.EventLog();
appLog.Source = "SQL SybaseConnector";
appLog.WriteEntry(str, EventLogEntryType.Information);
}
}
}
如果您要将它用作 COM 对象,请设置 [ComVisible(true)] 标志。编译项目并使用命令注册您的 dll
>regasm myTest.dll
然后用你的 vbscript 代码
Dim obj
Set obj = CreateObject("SybaseConnector.Connector")
Call obj.Connector("user","password","TCPIP{IP=host;ServerPort=2638}","northwind",command)