Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This would be trivial with a scalar function (UDF) rather than an SP. However, it should work easily enough - although if the SP is complex (i.e. FMT_ONLY can't inspect it 100%) then you might need to "help" it...</p> <p>Here's some dbml that I generated from a simplfied SP that returns an integer; you can edit the dbml via "open with... xml editor):</p> <pre><code>&lt;Function Name="dbo.foo" Method="foo"&gt; &lt;Parameter Name="inc" Type="System.Int32" DbType="Int" /&gt; &lt;Parameter Name="dataset" Type="System.String" DbType="VarChar(20)" /&gt; &lt;Return Type="System.Int32" /&gt; &lt;/Function&gt; </code></pre> <p>(note you obviously need to tweak the names and data-types).</p> <p>And here is the generated C#:</p> <pre><code>[Function(Name="dbo.foo")] public int foo([Parameter(DbType="Int")] System.Nullable&lt;int&gt; inc, [Parameter(DbType="VarChar(20)")] string dataset) { IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), inc, dataset); return ((int)(result.ReturnValue)); } </code></pre> <p>If your current SP uses SELECT (instead of RETURN), then the DBML will need to reflect this. You can fix this by hiding the implementation details, and providing a public wrapper in a partial class; for example:</p> <pre><code>&lt;Function Name="dbo.foo" Method="FooPrivate" AccessModifier="Private"&gt; &lt;Parameter Name="inc" Type="System.Int32" DbType="Int" /&gt; &lt;Parameter Name="dataset" Type="System.String" DbType="VarChar(20)" /&gt; &lt;ElementType Name="fooResult" AccessModifier="Internal"&gt; &lt;Column Name="value" Type="System.Int32" DbType="Int NOT NULL" CanBeNull="false" /&gt; &lt;/ElementType&gt; &lt;/Function&gt; </code></pre> <p>The above describes an SP that returns a single table with a single column; but I've made the SP "private" to the data-context, and the result-type "internal" to the assembly (hiding it):</p> <pre><code>[Function(Name="dbo.foo")] private ISingleResult&lt;fooResult&gt; FooPrivate( [Parameter(DbType="Int")] System.Nullable&lt;int&gt; inc, [Parameter(DbType="VarChar(20)")] string dataset) { IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), inc, dataset); return ((ISingleResult&lt;fooResult&gt;)(result.ReturnValue)); } </code></pre> <p>Now <em>in my own class file</em> I can add a new partial class (a new .cs file) in the correct namespace, that exposes the method more conveniently:</p> <pre><code>namespace MyNamespace { partial class MyDataContext { public int Foo(int? inc, string dataSet) { return FooPrivate(inc, dataSet).Single().value; } } } </code></pre> <p>(the namespace and context names need to be the same as the actual data-context). This adds a public method that hides the grungy details from the caller.</p> <p><strong>Don't</strong> edit the designer.cs file directly; your changes will be lost. Only edit either the dbml or partial classes.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload