我想通过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)
Additonal注意:批次(或其他)将由一个电话客户端执行,该客户端将使用正在呼叫的人的名字参数来调用批次(或程序)。 所以如果我可以在查询中构建参数,那就太好了。
答案 0 :(得分:0)
我有相同的任务,看起来你不能使用标准ADODB.Connection连接。我做了一些研究,发现你应该使用iAnywhere.Data.SQLAnywhere lib和它自己的SybaseConnector.Connector连接到Sybase。 您可以使用SybaseConnector创建自己的COM对象,将其注册到系统并创建新对象或生成带有语音字符串和查询的可执行文件,而不是使用批处理来调用vb vbscript文件。我可以和你分享来源。
设置Sybase IQ 15.4 dev版,然后在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)