0

I use <ScriptMethod(ResponseFormat:=ResponseFormat.Json)> to automatically format what I Return as json formatted output.

However, I think I'm doing more than necessary because I first dump the contents of a DataSet into a Dictionary, and I Return the Dictionary.

If I used aliases on the columns and wanted to output all columns, is there a way to simply Return the DataSet like I can with the Dictionary? If not, how can I do this with as few lines as possible?

Dim conn As New SqlConnection
conn.ConnectionString = Module1.DBConn2
Dim sqlCommand = New SqlCommand("Select id, column1, column2... From table1", conn)
    conn.Open()
    Dim sqlDataset As DataSet = New DataSet()
    Dim sqlDataAdapter As SqlDataAdapter = New SqlDataAdapter(sqlCommand)
    sqlDataAdapter.Fill(sqlDataset)
    conn.Close()

    Dim jsonDict(sqlDataset.Tables(0).Rows.Count - 1) As Dictionary(Of Object, Object)
    Dim i As Integer = 0
    For Each rs As DataRow In sqlDataset.Tables(0).Rows
        jsonDict(i) = New Dictionary(Of Object, Object)
        jsonDict(i).Add("id", rs.Field(Of Object)("id"))
        jsonDict(i).Add("column1", rs.Field(Of Object)("column1"))
        jsonDict(i).Add("column2", rs.Field(Of Object)("column2"))
        ...
    i = i + 1
Next
Return jsonDict

2 Answers2

0

you can get every row by id(primary key) : change return type to datatable

return sqlDataset.Tables(0);

this link can help you link

or this link

Community
  • 1
  • 1
Habib Zare
  • 1,206
  • 8
  • 17
0
1. pass the array of DataSets to a web service web method that
takes a DataSet as a parameter.

for example:

[webmethod]
public static void(Dataset[] a)
{
// do something
}

you can also do operation with xml to fill up dataset:

2. Use the DataSet.GetXML method (which returns an XML string representing
the DataSet) and pass that string to a web service web method. Then that
web method would declare a new DataSet and using the ReadXML method, it
could read the XML string into itself. You will need to load the XML string
into an XMLDocument and then pass it to an XMLNodeReader so that it can be
read into the DataSet using ReadXML.

3. fill up each dataset from the array. and pick a dataset through loop operation and after picking the dataset get the table in that dataset. finally you can get the row using the process as below(if needed):

DataRow dr;
Dataset ds;
DataTable dt;

dt = ds.Tables(0);
foreach(dr in ds.tables(0).rows){
// dr("ColName") to get value;
}
Rashedul.Rubel
  • 3,446
  • 25
  • 36