0

我想通过 CMD 调用的 VBScript 发出 SQL 请求。数据库是 Sybase 服务器,这就是问题所在,我找不到任何关于此的文档,只有 MS SQL 和类似的东西。

但它不一定是这样的复杂方式,如果有人知道一个在执行时会输出请求的小工具,我也会很高兴。

但主要目标是在程序(或脚本)执行时输出请求的数据。

更新

我发现了一些东西,这可能会有所帮助

Dim OdbcDSN
Dim connect, sql, resultSet

OdbcDSN = "******;UID=*******;PWD=*****"
Set connect = CreateObject("ADODB.Connection")
connect.Open OdbcDSN

sql="SELECT * FROM **********..********** WHERE ******* = 1 AND Name = %given parameter%"

resultSet.Close
connect.Close
Set connect = Nothing

WScript.Quit(0)

附加说明:批处理(或其他)将由电话客户端执行,该客户端将使用参数(或程序)调用批处理(或程序),即调用者的姓名。因此,如果我可以将参数构建到查询中,那就太好了。

4

1 回答 1

0

我有同样的任务,看起来你只是无法使用标准 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)
于 2013-11-14T16:01:49.830 回答