﻿<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>StrataFrame Forum » StrataFrame Application Framework - V1 » Business Objects and Data Access (How do I?)  » FillbyPrimaryKey from SPROC</title><generator>InstantForum 2017-1 Final</generator><description>StrataFrame Forum</description><link>http://forum.strataframe.net/</link><webMaster>StrataFrame Forum</webMaster><lastBuildDate>Wed, 09 Sep 2026 19:24:54 GMT</lastBuildDate><ttl>20</ttl><item><title>FillbyPrimaryKey from SPROC</title><link>http://forum.strataframe.net/FindPost24700.aspx</link><description>I love FillbyPrimaryKey with the overload that accepts a paramarray of keys.  But I would like to us a SPROC to get the data on the back end as there are a number of joins I'd prefer to handle there.&lt;br&gt;
&lt;br&gt;
What is the syntax for calling using FillbyStoreProcedure or  cmd.CommandType = StoredProcedure and having, as a parameter, a list(of T)&lt;br&gt;
&lt;br&gt;
( need to do this with both integer keys and system.guid keys )&lt;br&gt;
&lt;br&gt;
TIA&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
 &lt;br&gt;</description><pubDate>Fri, 25 Sep 2009 20:42:24 GMT</pubDate><dc:creator>Charles R Hankey</dc:creator></item><item><title>RE: FillbyPrimaryKey from SPROC</title><link>http://forum.strataframe.net/FindPost24743.aspx</link><description>Just posted my first shot at Generic methods on the Walkthrough referenced here.    Basically, it allows me to use one sub in my BaseBO for whatever data type of pk and there is also a refactor of the shared function in my Base.Utility class that converts a List(Of T) to a datatable with one column of type T&lt;br&gt;
&lt;br&gt;
I have been working a bit with LINQ and hope to post some samples on really basic stuff, working with SF types soon.  Getting my head around some of the Generic stuff is somehow helping with that.&lt;br&gt;
&lt;br&gt;
&lt;br&gt;</description><pubDate>Fri, 25 Sep 2009 20:42:24 GMT</pubDate><dc:creator>Charles R Hankey</dc:creator></item><item><title>RE: FillbyPrimaryKey from SPROC</title><link>http://forum.strataframe.net/FindPost24739.aspx</link><description>Well, this is some really cool stuff.  The third article also introduced the hierarchyid data type in SQL which is just bloody awesome!  I use hierarchies quite a lot and it was a pain in the...er...posterior.  I also notices that it appears that T-SQL now can do OOP type stuff now. I.e. a variable that is a hierarchyid has methods!  When did this happen?!?!  Anyway, I'm very excited to delve into this more and hope to install SQL Server 2008 soon....Now back to planing for the migration of my companies SQL Server 2000 servers to SQL Server 2005....sigh...</description><pubDate>Fri, 25 Sep 2009 14:41:30 GMT</pubDate><dc:creator>Greg McGuffey</dc:creator></item><item><title>RE: FillbyPrimaryKey from SPROC</title><link>http://forum.strataframe.net/FindPost24708.aspx</link><description>Walkthrough is posted on User Contributed Samples</description><pubDate>Thu, 24 Sep 2009 20:53:45 GMT</pubDate><dc:creator>Charles R Hankey</dc:creator></item><item><title>RE: FillbyPrimaryKey from SPROC</title><link>http://forum.strataframe.net/FindPost24705.aspx</link><description>Greg and I have already discussed this but I wanted to continue the discussion here for the benefit of other SF developers  :&lt;br&gt;
&lt;br&gt;
In SQL2008 the Table View Parameter has added an incredibly powerful tool for passing parameters into T-SQL sprocs.  An old friend, Kevin Goff, has written some very good stuff for Code Magazine on all this and he sent me some links about the topic in general and TVPs in particular&lt;br&gt;
&lt;br&gt;
[url]http://www.setfocus.com/TechnicalArticles/Articles/sql-server-2005-tsql-3.aspx[/url]&lt;br&gt;
 (go down to section 5....this converts a CSV to a table variable for subsequent join operations)&lt;br&gt;
&lt;br&gt;
[url]http://www.code-magazine.com/Article.aspx?quickid=0709031 [/url]&lt;br&gt;
(listings 12 and 13....this converts an XML string to a table variable)&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
[url]http://www.code-magazine.com/Article.aspx?quickid=0807041[/url]&lt;br&gt;
Finally, wrote about using the new table type in 2008 to pass a datatable as a parameter&lt;br&gt;
 (Tip #5)&lt;br&gt;
&lt;br&gt;
This last link shows how to pass a datatable as a parameter into a sproc, where it can be used to do one big honkin' insert.  &lt;br&gt;
&lt;br&gt;
I am particularly interested in doing this with a table of keys in order to accomplish the same thing as fillbyprimarykey() using the paramarray overload.  &lt;br&gt;
&lt;br&gt;
NOTE: This is only relevant to SQL2008 and that is sql10 - spent an embarassing amount of time last night wondering why I was getting errors executing scripts, for getting the sql express db I was in ( sql9  - like vb 9 latest version, right?  WRONG ) was sql2005.  &lt;br&gt;
&lt;br&gt;
Anyway, as soon as I get a complete walk-through on this done I'll post it .  There is a huge amount of potential here and I'm amazed I haven't run across this solution before, though I'm sure somebody mentioned it but I just didn't get the implications at the time.&lt;br&gt;
&lt;br&gt;
&lt;br&gt;</description><pubDate>Thu, 24 Sep 2009 15:05:48 GMT</pubDate><dc:creator>Charles R Hankey</dc:creator></item><item><title>RE: FillbyPrimaryKey from SPROC</title><link>http://forum.strataframe.net/FindPost24701.aspx</link><description>I haven't used the FillByStoredProcudure, but I use a lot of sprocs, for a lot of things.  I don't think that a .NET generic type (List) will mix well with SQL. I know it won't when using SqlCommand.Parameters.  Here is what the code might look like.&lt;br&gt;
&lt;br&gt;
SQL Sproc Example:&lt;br&gt;
[codesnippet]Create Proc testFill&lt;br&gt;
&amp;nbsp;&amp;nbsp;@key int&lt;br&gt;
AS&lt;br&gt;
&amp;nbsp;&amp;nbsp;Select table1.*&lt;br&gt;
&amp;nbsp;&amp;nbsp;From table1  Left Outer Join table2&lt;br&gt;
&amp;nbsp;&amp;nbsp;Where id = @key&lt;br&gt;
[/codesnippet]&lt;br&gt;
&lt;br&gt;
SF Fill procedure using the above sproc:&lt;br&gt;
[codesnippet]Public Sub FillByKey(ByVal key As Integer)&lt;br&gt;
&amp;nbsp;&amp;nbsp;Using cmd As New SqlCommand()&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;cmd.CommandType = StoredProcedure&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;cmd.CommandText = "testFill"&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;cmd.Parameters.AddWithValue("@key", key).SqlDataType = SqlDataType.int&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Me.FillDataTable(cmd)&lt;br&gt;
&amp;nbsp;&amp;nbsp;End Using&lt;br&gt;
End Sub[/codesnippet]&lt;br&gt;
&lt;br&gt;
Now, I have a feeling that it is possible that a sproc might not work out as well as you'd like. The reason being that you want to pass a list in. I.e. there will be an unknown number of keys, and as far as I know, SQL server doesn't do that well (if it does, I'd really like to know about it!). You might be able to use a CLR sproc that could be used to parse a string of delimited keys in, but an easier way is this.  You simply build the SQL statement with the appropriate number of parameters and then add those parameters, driven by the list. In order to handle the complex joins, you could either do them here or create a view and then query the view (probably the better way to go).&lt;br&gt;
&lt;br&gt;
Here is an example. I just hacked this out, so I might have messed up on some of the syntax or made some other mistake. Hopefully you'll get the idea.&lt;br&gt;
[codesnippet]&lt;br&gt;
'''&amp;lt;Summary&amp;gt;&lt;br&gt;
''' Fill the BO with the records that contain the indicated keys. &lt;br&gt;
''' This is generic so one method can handle both integers and Guids.&lt;br&gt;
'''&amp;lt;/Summary&amp;gt;&lt;br&gt;
Public Sub FillByKeyList(Of T)(keyList As List(Of T))&lt;br&gt;
&amp;nbsp;&amp;nbsp;'-- Validate that T of supported type.&lt;br&gt;
&amp;nbsp;&amp;nbsp;If T.GetType() IsNot GetType(Integer) AndAlso T.GetType() IsNot GetType(Guid) Then&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Throw New ArgumentException("The list must be of either integers or GUIDS.")&lt;br&gt;
&amp;nbsp;&amp;nbsp;End If&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;'-- Define a template that will be used to build the name of the parameter.&lt;br&gt;
&amp;nbsp;&amp;nbsp;'&amp;nbsp;&amp;nbsp;&amp;nbsp;{1} is replaced with a delimiter if needed and {0} with &lt;br&gt;
&amp;nbsp;&amp;nbsp;'&amp;nbsp;&amp;nbsp;&amp;nbsp;the index of the item in the list.&lt;br&gt;
&amp;nbsp;&amp;nbsp;Dim paramNameTemplate As String = "{1}@id_{0}"&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;'-- Build basic SQL statement. We'll use a view to make life easier.&lt;br&gt;
&amp;nbsp;&amp;nbsp;Dim sql As String&lt;br&gt;
&amp;nbsp;&amp;nbsp;sql = "Select * From ComplexJoinView"&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;'-- Build an In clause with parameters names.&lt;br&gt;
&amp;nbsp;&amp;nbsp;Dim keyParamList As String = String.Empty&lt;br&gt;
&amp;nbsp;&amp;nbsp;For idx As Integer = 0 To keyList.Count - 1&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Dim itemDelimiter As String = ","&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;If idx = 0 Then idx = String.Empty&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;keyParamList &amp;= String.Format(paramNameTemplate, idx, itemDelimiter)&lt;br&gt;
&amp;nbsp;&amp;nbsp;Next&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;'-- Add In clause to SQL&lt;br&gt;
&amp;nbsp;&amp;nbsp;sql &amp;= String.Format(" In({0})", keyParamList)&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;'-- Build SQL Command&lt;br&gt;
&amp;nbsp;&amp;nbsp;Using cmd As New SqlCommand()&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;cmd.CommandText = sql&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;'-- Add the parameters for each key&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;For idx As Integer = 0 To keyList.Count - 1&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;'-- Determine what the SQL Data Type is Based on generic type.&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Dim sqlType As SqlDataType&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;If T.GetType() Is GetType(Integer) Then&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;sqlType = SqlDataType.Integer&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ElseIf T.GetType() Is GetType(Guid) Then&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;sqlType = SqlDataType.UniqueIdentifier&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;End If&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;'-- Build name of this parameter&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Dim paramName As String = String.Format(paramNameTemplate, idx, String.Empty)&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;'-- Add the parameter&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;cmd.Parameters.AddWithValue(paramName, keyList(idx)).SqlDataType = sqlType&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Next&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;'-- Fill BO&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Me.FillDataTable(cmd)&lt;br&gt;
&amp;nbsp;&amp;nbsp;End Using&lt;br&gt;
End Sub[/codesnippet]&lt;br&gt;
&lt;br&gt;
Hopefully that will get you going or spark a useful idea!</description><pubDate>Thu, 24 Sep 2009 00:58:22 GMT</pubDate><dc:creator>Greg McGuffey</dc:creator></item></channel></rss>