Software development tutorials and demos lasting no more than 60 seconds.
No in-depth explanations, no off-topic rants, and no slides. One small topic per video - only code, console, config, and client.
All videos come with a corresponding blog entry, and all code is available on GitHub.
SQL Server Table Valued Parameters with .NET
Get link
Facebook
X
Pinterest
Email
Other Apps
-
privatestatic SqlDataRecord CreatePersonRecord(int id,string name){var metaData =new[]{new SqlMetaData("Id", SqlDbType.Int),new SqlMetaData("Name", SqlDbType.VarChar,50)};var record =new SqlDataRecord(metaData);
record.SetInt32(0, id);
record.SetString(1, name);return record;}private SqlDataRecord[] GetPersonRecords(){
retun new[]{ CreatePersonRecord(1,"John Doe"), CreatePersonRecord(2,"Jane Doe")};}using(var conn =new SqlConnection(cs)){
conn.Open();using(var comm = conn.CreateCommand()){
comm.CommandText ="dbo.TestPerson";
comm.CommandType = CommandType.StoredProcedure;var peopleParmsValue = GetPersonRecords();if(!peopleParmsValue.Any()){
peopleParmsValue =null;// Don't set the value to an empty array}
comm.Parameters.Add(new SqlParameter {
ParameterName ="@people",
Value = peopleParmsValue,
SqlDbType = SqlDbType.Structured,
TypeName ="dbo.PersonType"});using(var rdr = comm.ExecuteReader()){while(rdr.Read()){
Console.WriteLine("{0}: {1}", rdr["Id"], rdr["Name"]);}}}}
With .NET 5, you get a new concept called record . Lots of cool things, one of which is that you can serialize and deserialize with JSON just like a class, except you don't need to define properties one at a time, or even the body of the type. Apparently XML serialization doesn't quite work, so stick with JSON. View code on GitHub
rem dptnt.com/2009/04/how-to-add-date-time-stamp-to-jpeg-photos-using-free-software/ @ ECHO OFF SETLOCAL FOR / R %%G IN ( *.jpg ) DO CALL :process "%%G" GOTO :end :process SET _inname = %1 identify - format %%w %_inname% > dttmpfile set / p width = < dttmpfile Set / a pointsize = %width% / 50 rem echo ZZ >> dttempfile DEL dttmpfile ECHO Processing %_inname% ... convert %_inname% - gravity SouthEast - font Arial - pointsize %pointsize% - fill orange - annotate +%pointsize%+%pointsize% "%%[exif:DateTimeOriginal]" %_inname% EXIT / B :end
Rather than try to make it happen in one big command, here’s breaking it out into many commands. Whether or not you use the transaction is up to you and your use case – you could always have a “Completed” column and only set that to true after success, if you wanted to skip a transaction. First, insert a record, leaving the blob column as an empty byte array, making sure you have access to the primary key of the record you just inserted: using var conn = new SqlConnection ( _config . GetConnectionString ( " MyDB " ) ) ; await conn . OpenAsync ( ) . ConfigureAwait ( false ) ; using var tran = ( SqlTransaction ) await conn . BeginTransactionAsync ( ) ; int fileID ; using ( var comm = conn . CreateCommand ( ) ) { comm . Transaction = tran ; comm . CommandText = @" insert dbo.Files (FileName, FileContents) values (@FileName, 0x); ...
Comments
Post a Comment