0

I have a .Net dll which provides me exposed function RunSqlQuery(). This function returns System.Data.Dataset. I am calling this function from VBScript.

If I use the C# syntax of dataset in VBScript, I get error "Object required"

How can I get the data from the dataset in VBScript?

.NET Dll function signature:

public DataSet RunSqlQuery(string query);

My VBScript code:

On Error Resume Next
Dim objSQLDB
Dim dataset
Dim retVal

set objSQLDB = CreateObject("SQLManagement.SQLServer")
retVal = objSQLDB.Connect("server-address", "username", "password")

If retVal = 0 Then
    WScript.echo "Failed to connect: " & Err.Number & " " & Err.Description 
    WScript.Quit
End If

'Any of the below two lines does not help resolving Object required error
dataset = objSQLDB.RunSqlQuery("Select 1")
'set dataset = objSQLDB.RunSqlQuery("Select 1")

If Err.Number <> 0 Then
    WScript.echo "Error running query: " & Err.Number & " " & Err.Description 
    WScript.Quit
End If

' Stuck here : how to extract data from the dataset
' Below two lines does not work
' Error message: Microsoft VBScript runtime error: Object required

WScript.echo dataset.Tables.Count
WScript.echo dataset.Tables[0].Rows.Count

objSQLDB.Disconnect
objSQLDB = Nothing
Barshan Das
  • 3,677
  • 4
  • 32
  • 46
  • @JSteward How? The link you provided is about connecting to database from VBScript through ADO. After that the records are fetched through ADODB.Recordset. Here in my case it is a .Net System.Data.Dataset. Do you mean to say they are same? I do not think so. Educate me if I am wrong. – Barshan Das Sep 18 '19 at 18:16
  • Looks like my mistake, retracted, thought they were working with datasets as well. – JSteward Sep 18 '19 at 18:20
  • Possible duplicate of [Calling C# dll in vbscript](https://stackoverflow.com/questions/1451956/calling-c-sharp-dll-in-vbscript) – user692942 Sep 18 '19 at 22:25
  • @Lankymart no it is not. My question is not about calling a dll and returning any value, but particular to System.data.dataset. I am stuck at how to extract data from a dataset after the dll returns the dataset . – Barshan Das Sep 19 '19 at 03:19
  • What have you tried, just showing a comment in code saying `Stuck here:` with no actual attempt isn't very helpful. Have you actual tried to call some methods or properties of the `DataSet` object? Did you encounter an error when you tried? Can you show your attempts? – user692942 Sep 19 '19 at 07:09
  • Okay. Since I waned to put a short code in the Question and pin-point where exactly I am having problem, I did not put the entire code. Now, I have edited my question to include what I have tried already. I know how to use a C# System.Data.Dataset in a C# code. I already tried the same syntax in VBScript and that did not work. Do you know how to use a System.Data.Dataset in VBScript? Or, how do I convert a System.Data.Dataset returned by a .net dll in VBScript, so that I can access the data inside that? – Barshan Das Sep 19 '19 at 09:17
  • @BarshanDas `dataset` variable is an object reference so you need to use `Set dataset = objSQLDB.RunSqlQuery("Select 1")` hence the `Object required` error. – user692942 Sep 19 '19 at 11:05
  • @BarshanDas `System.Data.DataSet` is not a COM Visible object, so it's not possible to use it on VBScript side. Your best option is referencing ADODB in your .NET end and returning a Recordset from there. – Kul-Tigin Sep 19 '19 at 11:06
  • @Lankymart your analysis does not help. I have tried putting Set dataset earlier and I get same Object required error. Please see my updated code. It does not make any difference. Please remove the duplicate mark from my question, because it is not a duplicate one. – Barshan Das Sep 19 '19 at 19:07
  • @Kul-Tigin I find your comment useful. What you are saying makes sense to me. That basically means System.Data.DataSet is a closed door for me. Fine, I will try with ADODB Recordset. – Barshan Das Sep 19 '19 at 19:10
  • Anyone facing the same problem, there are two ways. Either you can go for ADODB Recordset. Alternatively, you have to write a wrapper over Dataset to give you the data you want. I will add a full answer later. – Barshan Das Oct 17 '19 at 11:03

0 Answers0