Class TableType
Custom type to use on Sql Parameters with approach Table Type.
Inheritance
System.Object
TableType
Inherited Members
System.Object.Equals(System.Object)
System.Object.Equals(System.Object, System.Object)
System.Object.GetHashCode()
System.Object.GetType()
System.Object.MemberwiseClone()
System.Object.ToString()
System.Object.ReferenceEquals(System.Object, System.Object)
Namespace: StoneCo.Framework.Data.SqlServer
Assembly: cs.temp.dll.dll
Syntax
public class TableType
Properties
SqlDbType
Default type setted.
Declaration
public SqlDbType SqlDbType { get; }
Property Value
Type | Description |
---|---|
SqlDbType | SqlDbType.Structured |
Table
Table used on query.
Declaration
public DataTable Table { get; }
Property Value
Type | Description |
---|---|
DataTable | Instance of DataTable |
TypeName
Name of object type created on database.
Declaration
public string TypeName { get; }
Property Value
Type | Description |
---|---|
System.String | dbo.CustomTableType |
Methods
Create(DataTable, String)
Create new TableType to use on query.
Declaration
public static TableType Create(DataTable table, string typeName)
Parameters
Type | Name | Description |
---|---|---|
DataTable | table | |
System.String | typeName |
Returns
Type | Description |
---|---|
TableType |
Examples
In this example, use FooQuery class as result of the query.
public class FooQuery : ValueObject<FooQuery>
{
public FooQuery(string name) => Name = name;
public string Name { get; private set; }
}
Bar class used in this example.
public class Bar
{
private IDbContextFoo Context { get; }
public Bar(IDbContextFoo context) => Context = context;
public async Task<IEnumerable<FooQuery>> GetFooQuery(string name)
{
var values = new List<string>() { "Stone Co" };
var table = NewTable(values);
var tableType = TableType.Create(table, "dbo.CustomTableType")
var parameters = new Dictionary<string, object>();
var sql = @"SELECT name FROM Foo WHERE name in (SELECT FieldValue FROM @Table)";
parameters.Add("@Table", tableType);
return await Context.ExecuteRawSqlAsync<FooQuery>(sql, parameters);
}
private DataTable NewTable(IEnumerable<string> values)
{
var table = new DataTable();
table.Columns.Add(new DataColumn("FieldValue", typeof(string)));
foreach (var value in values)
{
var row = table.NewRow();
row["FieldValue"] = value;
table.Rows.Add(row);
}
}
}
Create TableType on database.
CREATE TYPE CustomTableType AS TABLE([FieldValue] VARCHAR(30))