User defined table types can be passed on Ado.Net with the use of DataTables
Dal code
- public static int AddBulkData(Guid itemGuid, List<SettingsPacket> data)
- {
- int result = 0;
- DataTable settingsPacketsTable = new DataTable("dbo.udtTypeName");
- /*
- [Col1] INT,
- [ColGuid] UNIQUEIDENTIFIER,
- [ColData] VARBINARY (MAX),
- [ColBit] BIT
- */
- settingsPacketsTable.Columns.Add("Col1", typeof(byte));
- settingsPacketsTable.Columns.Add("ColGuid", typeof(Guid));
- settingsPacketsTable.Columns.Add("ColData", typeof(byte[]));
- settingsPacketsTable.Columns.Add("ColBit", typeof(bool));
- foreach (SettingsPacket packet in data)
- {
- DataRow row = settingsPacketsTable.NewRow();
- row["Col1"] = packet.SettingStatus;
- row["ColGuid"] = itemGuid;
- row["ColData"] = packet.Data;
- row["ColBit"] = packet.IsGet;
- settingsPacketsTable.Rows.Add(row);
- }
- using (ISqlDbConnection connection = SqlDbConnectionProvider.GetConnection("ConnStringName"))
- using (ISqlCommandWrapper cmd = connection.CreateCommand("SprocName"))
- {
- cmd.AddInParameter("@UdtParamName", "dbo.udtTypeName", settingsPacketsTable);
- cmd.AddReturnParameter("@Return", SqlDbType.Int, 4);
- connection.ExecuteNonQuery(cmd);
- result = (int)cmd.GetParameterValue("@Return");
- }
- return result;
- }
Things to remember:
– The order of data table columns should match the order of the udtType,
– Ado.Net will not lookup udt columns by name