Mamine
asked on
How to fill a dataset from an SQL user-defined function
I have an SQL inline udf which takes 3 parameters and returns a table of results. (It works in SQL query analyser so I know there's no probs on the SQL end). I'm trying to get the resulting table into a dataview:
DbCommand.CommandText = "SELECT * FROM dbo.[udfMyUDF](1,1,65)"
DbDataAdapter.SelectComman d = DbCommand
DbDataSet = New DataSet
DbDataAdapter.Fill(DbDataS et)
The connection, command & adapter all seem to go through fine but when I do the fill I get 0 rows and no table. Can anyone help ?
DbCommand.CommandText = "SELECT * FROM dbo.[udfMyUDF](1,1,65)"
DbDataAdapter.SelectComman
DbDataSet = New DataSet
DbDataAdapter.Fill(DbDataS
The connection, command & adapter all seem to go through fine but when I do the fill I get 0 rows and no table. Can anyone help ?
wouldnt you want to use executescalar ? your function is returning a value no ?
ASKER
I thought executescalar was for simple single value results. Will it cope with a table being returned ? And if so how do I get it into a Dataset ?
DbCommand.TableMappings.Ad d("Table", "tblName")
DbDataSet = New DataSet
DbDataSet.Tables.Add("tblN ame")
DbDataAdapter.Fill(DbDataS et, "tblName")
DbDataSet = New DataSet
DbDataSet.Tables.Add("tblN
DbDataAdapter.Fill(DbDataS
it should default to "table1" no ? perhaps because its a table object being returned from the function the dataadapter is going a bit nuts ?
You shouldn't use Scalar for this because it is an entire Result Set you are seeking.
All I know...is when I create datasets in this way (Like when importing from Excel...I need to define the TableMappings as shown above.
ASKER
In this:
DbCommand.TableMappings.Ad d("Table", "tblName")
DbDataSet = New DataSet
DbDataSet.Tables.Add("tblN ame")
DbDataAdapter.Fill(DbDataS et, "tblName")
is the tblName the name of the table in the sql database ? If so, I can't use it because the user-defined function is putting together a table from several related tables.... unless there is a way of naming the output table from the udf ?
DbCommand.TableMappings.Ad
DbDataSet = New DataSet
DbDataSet.Tables.Add("tblN
DbDataAdapter.Fill(DbDataS
is the tblName the name of the table in the sql database ? If so, I can't use it because the user-defined function is putting together a table from several related tables.... unless there is a way of naming the output table from the udf ?
No...basically whatever your query returns from DbCommand will be stored in the Table called tblName...in your case the results of your UDF...so you could do...
DbCommand.CommandText = "SELECT * FROM dbo.[udfMyUDF](1,1,65)"
DbCommand.TableMappings.Ad d("Table", "tblUDFRes ults")
DbDataSet = New DataSet
DbDataSet.Tables.Add("tblU DFResults" )
DbDataAdapter.SelectComman d = DbCommand
DbDataAdapter.Fill(DbDataS et, "tblUDFResults")
DbCommand.CommandText = "SELECT * FROM dbo.[udfMyUDF](1,1,65)"
DbCommand.TableMappings.Ad
DbDataSet = New DataSet
DbDataSet.Tables.Add("tblU
DbDataAdapter.SelectComman
DbDataAdapter.Fill(DbDataS
ASKER
that sounded like it might've solved the problem but if I type in:
DbCommand.TableMappings.Ad d("Table", "tblUDFRes ults")
I get a syntax error: TableMappings is not a member of DbCommand. I looked up TableMappings and MSHelp said it was a member of DataAdapter, so I did this instead:
DbCommand.CommandText = "SELECT * FROM dbo.[udfMyUDF](1,1,65)"
DbDataAdapter.TableMapping s.Add("Tab le", "tblUDFResults")
DbDataSet = New DataSet
DbDataSet.Tables.Add("tblU DFResults" )
DbDataAdapter.SelectComman d = DbCommand
DbDataAdapter.Fill(DbDataS et, "tblUDFResults")
But got the same error message: Missing SourceTable mapping: 'tblUDFResults' <groan!>
So near and yet so far !! This is really frustrating!
DbCommand.TableMappings.Ad
I get a syntax error: TableMappings is not a member of DbCommand. I looked up TableMappings and MSHelp said it was a member of DataAdapter, so I did this instead:
DbCommand.CommandText = "SELECT * FROM dbo.[udfMyUDF](1,1,65)"
DbDataAdapter.TableMapping
DbDataSet = New DataSet
DbDataSet.Tables.Add("tblU
DbDataAdapter.SelectComman
DbDataAdapter.Fill(DbDataS
But got the same error message: Missing SourceTable mapping: 'tblUDFResults' <groan!>
So near and yet so far !! This is really frustrating!
ASKER
Incidentally, what does the first "Table" parameter the TableMappings.Add refer to ?
ASKER
Taking another tack ... do I really need a DataAdapter at all ? All I want to do is get the table into a dataset (I don't ever want to update it) ... is there another way to read it in ?
Well, if you you want to read the data, you could use an SqlDataReader and populate a DataTable yourself.
ASKER
My understanding is that the datareader reads one row at a time - but the udf returns a whole table so I don't see how this will work ..... unless of course you know better ?
ASKER
Going back to the mapping problem ... if I generate a typed dataset will that solve it ? (Seems a long way round tho')
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Oops, forgot to mention that Read() moves the cursor to the next record (not a single column or byte). Check out the documentation in MSDN.
ASKER