<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>.NET - TheCodeBuzz</title>
	<atom:link href="https://thecodebuzz.com/category/net/feed/" rel="self" type="application/rss+xml" />
	<link>https://thecodebuzz.com</link>
	<description>Best Practices for Software Development</description>
	<lastBuildDate>Sat, 09 Mar 2024 22:09:06 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://thecodebuzz.com/wp-content/uploads/2022/11/cropped-android-chrome-512x512-1-1-51x51.jpg</url>
	<title>.NET - TheCodeBuzz</title>
	<link>https://thecodebuzz.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Role of hostfxr.dll In Application Lifecycle -Guidelines and Best Practices</title>
		<link>https://thecodebuzz.com/role-of-hostfxr-dll-application-lifecycle-guidelines-and-best-practices/</link>
					<comments>https://thecodebuzz.com/role-of-hostfxr-dll-application-lifecycle-guidelines-and-best-practices/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Tue, 21 Nov 2023 22:27:12 +0000</pubDate>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[hostfxr.dll]]></category>
		<guid isPermaLink="false">https://www.thecodebuzz.com/?p=29114</guid>

					<description><![CDATA[<p>Role of hostfxr.dll In Application Lifecycle -Guidelines and Best Practices Today in this article, we will understand the role of hostfxr.dll in application lifecycle and learn few Guidelines and Best Practices around its usage. New .NET ecosystem is already a lightweight abstraction layer around all complex components of .NET legacy. This lets you use only [&#8230;]</p>
<p>The post <a href="https://thecodebuzz.com/role-of-hostfxr-dll-application-lifecycle-guidelines-and-best-practices/">Role of hostfxr.dll In Application Lifecycle -Guidelines and Best Practices</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></description>
										<content:encoded><![CDATA[<h1 class="wp-block-heading">Role of hostfxr.dll In Application Lifecycle -Guidelines and Best Practices</h1>



<figure class="wp-block-image size-full is-resized"><img fetchpriority="high" decoding="async" width="566" height="487" src="https://www.thecodebuzz.com/wp-content/uploads/2023/11/hostfxrdll_.jpg" alt="" class="wp-image-29118" style="width:469px;height:auto" srcset="https://thecodebuzz.com/wp-content/uploads/2023/11/hostfxrdll_.jpg 566w, https://thecodebuzz.com/wp-content/uploads/2023/11/hostfxrdll_-300x258.jpg 300w" sizes="(max-width: 566px) 100vw, 566px" /></figure>



<p>Today in this article, we will understand the role of hostfxr.dll in application lifecycle and learn few Guidelines and Best Practices around its usage.</p>



<p></p>



<p>New .NET ecosystem is already a lightweight abstraction layer around all complex components of .NET legacy. This lets you use only components which are needed for your application in declarative manner.</p>



<p></p>



<p></p>



<div data-schema-only="false" class="wp-block-aioseo-faq"><h3 class="aioseo-faq-block-question">What is hostfxr.dll</h3><div class="aioseo-faq-block-answer">
<p><code>hostfxr</code> is basically Host Framework Execution Host, a crucial component in the .NET Core runtime.</p>



<p></p>
</div></div>



<p>The <code>hostfxr.dll</code> file is core component in .NET Applicaiton (specifically .NET Core 3.1, or.NET6 onwards). </p>



<p></p>



<p>It&#8217;s responsible for creating .NET runtime within an application.  </p>



<p></p>



<p>This component is responsible for initializing <a href="https://www.thecodebuzz.com/hostfxr-dll-could-not-be-found-net-core-fatal-error/">required libraries</a> Example-  CoreCLR etc. , hosting application and managing the lifecycle, and managing the execution of .NET applications by interfacing with the operating system.</p>



<p></p>



<h2 class="wp-block-heading">Role of hostfxr.dll Components </h2>



<p></p>



<p>When you build a .NET application, the <code>hostfxr.dll</code> plays a crucial role in bootstrapping the application&#8217;s runtime environment.</p>



<p></p>



<h2 class="wp-block-heading">Bootstrapping the Applicaiton Runtime</h2>



<p></p>



<ul class="wp-block-list">
<li>H<code>ostfxr.dll</code> serves as the entry point for .NET Core applications. </li>
</ul>



<p></p>



<ul class="wp-block-list">
<li>It performs the initial setup, loading the necessary runtime components and preparing the environment for executing managed code.</li>
</ul>



<p></p>



<ul class="wp-block-list">
<li><code>hostfxr.dll</code> loads necessary system libraries and core components, including CoreCLR, i.e <code>coreclr.dll</code> and other required runtime libraries.</li>
</ul>



<p></p>



<h2 class="wp-block-heading">Runtime Components Configuration and Initialization</h2>



<p></p>



<p>Being an entry point, it reads the application&#8217;s runtime configuration.</p>



<ul class="wp-block-list">
<li>Load framework version</li>
</ul>



<p></p>



<ul class="wp-block-list">
<li>Read Runtime configurations</li>
</ul>



<p></p>



<ul class="wp-block-list">
<li>Identify paths to runtime components</li>
</ul>



<p></p>



<ul class="wp-block-list">
<li>Loads core libraries example <code>coreclr.dll</code> (the CoreCLR runtime) and other essential runtime components.</li>
</ul>



<p></p>



<ul class="wp-block-list">
<li>Initializes the  runtime, which provides added 
<ul class="wp-block-list">
<li>Just-In-Time (JIT) compilation, </li>



<li>garbage collection(GC) </li>



<li>other runtime functionalities.</li>
</ul>
</li>
</ul>



<p></p>



<h2 class="wp-block-heading">Application Hosting Lifecycle Management</h2>



<p></p>



<ul class="wp-block-list">
<li><code>hostfxr.dll</code> activates the .NET application by creating an application domain and providing the necessary execution context for the application to start executing managed code.</li>
</ul>



<p></p>



<ul class="wp-block-list">
<li><code>hostfxr</code> manages the application&#8217;s lifecycle, handling loading, unloading, and execution of managed code within the designated runtime environment.</li>
</ul>



<p></p>



<ul class="wp-block-list">
<li><code>hostfxr.dll</code> manages error messages, diagnostics, and logging and also provides mechanisms for reporting errors or failures while application execution. </li>
</ul>



<p></p>



<ul class="wp-block-list">
<li><code>hostfxr.dll</code> manages version compatibility and allows to management of multiple versions of the .NET Core runtime.</li>
</ul>



<p></p>



<ul class="wp-block-list">
<li><code>hostfxr.dll</code> interacts with the operating system (Cloud, Non-Cloud system) to manage resources, memory, and process execution. It enables portability supporting multiple operating systems.</li>
</ul>



<p></p>



<p></p>



<h2 class="wp-block-heading">How to install and use hostfxr.dll</h2>



<p></p>



<p>hostfxr.dll is actually a part of the .NET Core runtime installation and hence simply will be available on target machine based on the .NET Core runtime requirements on the machine. </p>



<p></p>



<h3 class="wp-block-heading">Configuration on Physical server</h3>



<p></p>



<p>Please follow below steps to ensure right .NET core version and all its dependencies are deployed.</p>



<p></p>



<ul class="wp-block-list">
<li><strong>On Windows:</strong>
<ul class="wp-block-list">
<li>Download the .NET Core runtime installer or SDK from the official <a href="https://dotnet.microsoft.com/en-us/download" target="_blank" rel="noopener" title="">.NET </a>website.</li>
</ul>
</li>
</ul>



<p></p>



<ul class="wp-block-list">
<li><strong>On Linux:</strong>
<ul class="wp-block-list">
<li>use apt-get (<strong>Ubuntu</strong>), yum (<strong>CentOS</strong>), or others to install the .NET Core runtime. </li>



<li>Ensure to install the runtime packages that include <code>hostfxr.dll</code>.</li>
</ul>
</li>
</ul>



<p></p>



<h3 class="wp-block-heading"><em style="">Self-Cont</em><em>ained Vs Framework Dependent Deployment</em></h3>



<p></p>



<p>As we discussed above, If the target machine has the required .NET version already installed then the application built using .NET should work without any issue. This approach called as <strong><em>Framework Dependent Deployment</em></strong>.</p>



<p></p>



<p>If the required .NET <a href="https://www.thecodebuzz.com/get-sdk-and-runtime-version-net-core/">core SDK/runtime is not installed</a> then one can follow any of the below approaches discussed. An approach using&nbsp;<strong><em>Self-content deployment</em></strong>&nbsp;doesn’t require a .NET Core&nbsp;<strong>SDK/Runtime&nbsp;</strong>to be installed on the target machines.</p>



<p></p>



<p>For more details, please visit below article,</p>



<p><a href="https://www.thecodebuzz.com/self-contained-vs-framework-dependent-deployment-hostfxr-fdd-vs-scd/">Self Contained Vs Framework Dependent Deployment -Guidelines</a></p>



<p></p>



<p></p>



<p>Once .NET core is installed successfully, please cross verify the version using below commands,</p>



<figure class="wp-block-image size-large"><img decoding="async" width="1024" height="890" src="https://www.thecodebuzz.com/wp-content/uploads/2023/11/Get-SDK-and-Runtime-version-of-NET-Core-Manual-and-Programmatically-1024x890.jpg" alt="hostfxr dll download" class="wp-image-29116" srcset="https://thecodebuzz.com/wp-content/uploads/2023/11/Get-SDK-and-Runtime-version-of-NET-Core-Manual-and-Programmatically-1024x890.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2023/11/Get-SDK-and-Runtime-version-of-NET-Core-Manual-and-Programmatically-300x261.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2023/11/Get-SDK-and-Runtime-version-of-NET-Core-Manual-and-Programmatically-768x668.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2023/11/Get-SDK-and-Runtime-version-of-NET-Core-Manual-and-Programmatically.jpg 1380w, https://thecodebuzz.com/wp-content/uploads/2023/11/Get-SDK-and-Runtime-version-of-NET-Core-Manual-and-Programmatically-598x520.jpg 598w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<p></p>



<p></p>



<h3 class="wp-block-heading">Configuration on Cloud server</h3>



<p></p>



<p>In the context of cloud generally, you will specify the container image.  </p>



<p>For application with container images, especially when creating Docker images for .NET applications, <code>hostfxr.dll</code> is not necessary.</p>



<p></p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p>The Docker images for .NET applications will <em><strong>include the necessary runtime files and dependencies provided by Microsoft in their official image</strong></em>s.</p>



<p></p>
</blockquote>



<ul class="wp-block-list">
<li>Docker image for a .NET applicatio, if using an official .NET image. </li>



<li>These base images already contain the required <code>hostfxr.dll</code> and other runtime components, allowing your application to run within the container without explicitly including this file.</li>
</ul>



<p></p>



<h4 class="wp-block-heading">Sample Docker file</h4>



<p></p>



<p>For example, using a Dockerfile for a .NET Core application might look like this:</p>



<p></p>



<pre class="wp-block-code"><code># Use the official .NET SDK image
FROM mcr.microsoft.com/dotnet/sdk:6.0

# Copy the application files to the container and build the application
WORKDIR /app
COPY . ./
RUN dotnet publish -c Release -o out



# Run the application
ENTRYPOINT &#91;"dotnet", "TheCodeBuzz.dll"]
</code></pre>



<p></p>



<p>In above example, the base image <code><strong><em>mcr.microsoft.com/dotnet/aspnet:6.0</em></strong></code> already contains <code>hostfxr.dll</code> and other required runtime files.</p>



<p></p>



<p></p>



<p style="font-size:18px">Do you have any <strong>comments or ideas or any better </strong>suggestions to share?</p>



<p class="has-small-font-size"></p>



<p style="font-size:18px">Please sound off your comments below.</p>



<p class="has-medium-font-size"></p>



<p class="has-medium-font-size"><strong>Happy Coding </strong>!!</p>



<p></p>



<p></p>



<hr>



<p class=""></p>



<p class="has-background" style="background-color:#b6d9ac;font-size:18px"><br>Please <strong><em>bookmark </em></strong>this page and <em><strong>share </strong></em>it with your friends.                                                    Please <a href="https://www.thecodebuzz.com/subscription/" target="_blank" rel="noreferrer noopener"><em><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-luminous-vivid-orange-color"><strong>Subscribe</strong> </mark></em></a>to the blog to receive notifications on freshly published (2025) best practices and guidelines for software design and development.</p>




<br>



<hr>



<p class=""></p>



<p></p><p>The post <a href="https://thecodebuzz.com/role-of-hostfxr-dll-application-lifecycle-guidelines-and-best-practices/">Role of hostfxr.dll In Application Lifecycle -Guidelines and Best Practices</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://thecodebuzz.com/role-of-hostfxr-dll-application-lifecycle-guidelines-and-best-practices/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>SqlBulkCopy &#8211; Quick and Efficient Bulk Data Insert</title>
		<link>https://thecodebuzz.com/sqlbulkcopy-bulk-data-insert-quick-efficient-csharp-net/</link>
					<comments>https://thecodebuzz.com/sqlbulkcopy-bulk-data-insert-quick-efficient-csharp-net/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Sat, 04 Nov 2023 00:41:13 +0000</pubDate>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SqlBulkCopy]]></category>
		<guid isPermaLink="false">https://www.thecodebuzz.com/?p=28664</guid>

					<description><![CDATA[<p>SqlBulkCopy &#8211; Quick and Efficient Bulk Data Insert SqlBulkCopy is a very useful component in the .NET framework used mainly for efficient bulk-inserting large amounts of data into SQL Server databases. Compared to traditional row-by-row insert operations SqlBulkCopy allows high-performance bulk insert insert by minimizing the network round trips and database interaction. SqlBulkCopy is very [&#8230;]</p>
<p>The post <a href="https://thecodebuzz.com/sqlbulkcopy-bulk-data-insert-quick-efficient-csharp-net/">SqlBulkCopy – Quick and Efficient Bulk Data Insert</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></description>
										<content:encoded><![CDATA[<h1 class="wp-block-heading">SqlBulkCopy &#8211; Quick and Efficient Bulk Data Insert</h1>



<figure class="wp-block-image size-large"><img decoding="async" width="1024" height="417" src="https://www.thecodebuzz.com/wp-content/uploads/2023/12/SqlBulkCopy-c-example-1024x417.jpg" alt="SqlBulkCopy c# example " class="wp-image-29323" srcset="https://thecodebuzz.com/wp-content/uploads/2023/12/SqlBulkCopy-c-example-1024x417.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2023/12/SqlBulkCopy-c-example-300x122.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2023/12/SqlBulkCopy-c-example-768x313.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2023/12/SqlBulkCopy-c-example-785x320.jpg 785w, https://thecodebuzz.com/wp-content/uploads/2023/12/SqlBulkCopy-c-example.jpg 1275w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<p><strong>SqlBulkCopy </strong>is a very useful component in the .NET framework used mainly for efficient bulk-inserting large amounts of data into SQL Server databases. </p>



<p></p>



<p>Compared to traditional row-by-row insert operations SqlBulkCopy allows high-performance bulk insert insert by minimizing the network round trips and database interaction.</p>



<p></p>



<p><strong>SqlBulkCopy</strong> is very adaptable and works with DataReader, DataTable, and other custom data structures for different scenarios.</p>



<p></p>



<p></p>



<p></p>



<h2 class="wp-block-heading">Synchronous SqlBulkCopy C# for Bulk insert</h2>



<p></p>



<p>Let&#8217;s see the below example to demonstrate the <strong>SqlBulkCopy</strong> usage.</p>



<p></p>



<p>In the below code, we have two source tables i. Source Table and Destination Table placeholder which need to be inserted into SQL. </p>



<p></p>



<p>We are establishing a SqlConnection to SQL Server,</p>



<p></p>



<pre class="wp-block-preformatted"> using SqlConnection connection = new SqlConnection(conString);
 connection.Open();
</pre>



<p>Using SQL transactions for each DataTable, you create parameterized SQL queries and use <strong><a href="https://learn.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlbulkcopy?view=dotnet-plat-ext-7.0" target="_blank" rel="noreferrer noopener">SqlBulkCopy </a></strong>to insert the data.</p>



<p></p>



<pre class="wp-block-preformatted">using SqlTransaction transaction = connection.BeginTransaction();
///your logic
transaction.Commit();</pre>



<p></p>



<p>For each DataTable, you create parameterized SQL queries and use <strong><a href="https://learn.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlbulkcopy?view=dotnet-plat-ext-7.0" target="_blank" rel="noreferrer noopener">SqlBulkCopy </a></strong>to insert the data.</p>



<p></p>



<pre class="wp-block-preformatted">         using SqlBulkCopy bulkCopy = 
         new SqlBulkCopy(connection, SqlBulkCopyOptions.Default, transaction);

 
         bulkCopy.DestinationTableName = destTableName;
         await bulkCopy.WriteToServerAsync(sourceTable);</pre>



<p></p>



<p>Since we are using transactions, please commit the changes to make the update final.</p>



<p></p>



<p>Please Close the SqlConnection post all processing.</p>



<p></p>



<p>Ex.</p>



<pre class="wp-block-preformatted">         using SqlConnection connection = new SqlConnection(conString);
         connection.Open();

          /// your logicc here 

         connection.Close();</pre>



<p>Here is the complete code,</p>



<p></p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1024" height="448" src="https://www.thecodebuzz.com/wp-content/uploads/2023/11/Synchronous-SqlBulkCopy-for-Bulk-insert-1024x448.jpg" alt="c# SqlBulkCopy " class="wp-image-28690" srcset="https://thecodebuzz.com/wp-content/uploads/2023/11/Synchronous-SqlBulkCopy-for-Bulk-insert-1024x448.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2023/11/Synchronous-SqlBulkCopy-for-Bulk-insert-300x131.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2023/11/Synchronous-SqlBulkCopy-for-Bulk-insert-768x336.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2023/11/Synchronous-SqlBulkCopy-for-Bulk-insert-1536x671.jpg 1536w, https://thecodebuzz.com/wp-content/uploads/2023/11/Synchronous-SqlBulkCopy-for-Bulk-insert-2048x895.jpg 2048w, https://thecodebuzz.com/wp-content/uploads/2023/11/Synchronous-SqlBulkCopy-for-Bulk-insert-785x343.jpg 785w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>



<p></p>



<p></p>



<h2 class="wp-block-heading">Asynchronous c# SqlBulkCopy for Bulk insert</h2>



<p></p>



<p>Let&#8217;s see the below example to demonstrate the <strong>SqlBulkCopy</strong> usage,</p>



<p></p>



<p>The below reusable <a href="https://www.thecodebuzz.com/moq-to-mock-asynchronous-method-net-core-unit-tests/">method can be called asynchronously</a> from the Calle side and can take advantage of multithreading.</p>



<p></p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1024" height="430" src="https://www.thecodebuzz.com/wp-content/uploads/2023/11/SqlBulkCopy-Bulk-Data-Insert-efficient--1024x430.jpg" alt="" class="wp-image-28682" srcset="https://thecodebuzz.com/wp-content/uploads/2023/11/SqlBulkCopy-Bulk-Data-Insert-efficient--1024x430.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2023/11/SqlBulkCopy-Bulk-Data-Insert-efficient--300x126.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2023/11/SqlBulkCopy-Bulk-Data-Insert-efficient--768x322.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2023/11/SqlBulkCopy-Bulk-Data-Insert-efficient--1536x645.jpg 1536w, https://thecodebuzz.com/wp-content/uploads/2023/11/SqlBulkCopy-Bulk-Data-Insert-efficient--2048x860.jpg 2048w, https://thecodebuzz.com/wp-content/uploads/2023/11/SqlBulkCopy-Bulk-Data-Insert-efficient--785x330.jpg 785w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>



<p></p>



<p></p>



<p>Please note that there are no major differences between synchronous and asynchronous calls except use of the <strong><em>await </em></strong>keyword and method return type as Task.</p>



<p></p>



<pre class="wp-block-preformatted"></pre>



<p></p>



<p></p>



<h2 class="wp-block-heading">Best Practices of Using to Use SqlBulkCopy</h2>



<p></p>



<p>Below are a few best practices for using SqlBulkCopy,</p>



<p></p>



<ul class="wp-block-list">
<li><strong>Ensure Proper Data Validation</strong>:
<ul class="wp-block-list">
<li>Validate data integrity before performing bulk inserts to avoid errors. </li>



<li>This includes data type compatibility and constraints.</li>
</ul>
</li>
</ul>



<p></p>



<ul class="wp-block-list">
<li><strong>Use Appropriate Column Mapping</strong>:
<ul class="wp-block-list">
<li>Ensure that the source data columns are correctly mapped to the destination table </li>



<li>Mismatched columns can lead to data corruption.</li>
</ul>
</li>
</ul>



<p></p>



<ul class="wp-block-list">
<li><strong>Consider Using Transactions</strong>:
<ul class="wp-block-list">
<li>Wrap your bulk copy operation in a transaction (SqlTransaction). </li>



<li>This allows you to commit or roll back the entire operation as a single transaction, ensuring data consistency.</li>
</ul>
</li>
</ul>



<p></p>



<ul class="wp-block-list">
<li><strong>Handle Exceptions Gracefully</strong>:
<ul class="wp-block-list">
<li>Implement <a href="https://www.thecodebuzz.com/best-practices-for-handling-exception-in-net-core-2-1/" target="_blank" rel="noopener" title="Best Practices for Exception Handling in .NET Core">exception handling</a> to capture any errors that occur during the bulk copy operation. This will help you identify and address issues promptly.</li>
</ul>
</li>
</ul>



<p></p>



<ul class="wp-block-list">
<li><strong>Optimize Destination Table</strong>:
<ul class="wp-block-list">
<li>Disable non-clustered indexes, triggers, and constraints on the destination table before performing the bulk copy operation. </li>



<li>Rebuild or re-enable them after the operation to improve performance.</li>
</ul>
</li>
</ul>



<p></p>



<ul class="wp-block-list">
<li><strong>Consider Using Temporary Tables</strong>:
<ul class="wp-block-list">
<li>In some cases, using temporary tables for bulk inserts and then transferring the data to the final table can improve performance. </li>



<li>This is especially useful when dealing with large datasets in a production environment.</li>
</ul>
</li>
</ul>



<p></p>



<ul class="wp-block-list">
<li><strong>Asynchronous and Parallelism</strong>:
<ul class="wp-block-list">
<li>Depending on the hardware and the nature of your data, consider <a href="https://www.thecodebuzz.com/understanding-async-and-await-api-asynchronous-programming/" target="_blank" rel="noopener" title="Sync Vs Async Multithreading  – Asynchronous Programming">asynchronous </a>and parallelizing your bulk copy operations to take full advantage of multi-core processors.</li>
</ul>
</li>
</ul>



<p></p>



<p></p>



<p></p>



<p style="font-size:18px">Do you have any <strong>comments or ideas or any better </strong>suggestions to share?</p>



<p class="has-small-font-size"></p>



<p style="font-size:18px">Please sound off your comments below.</p>



<p class="has-medium-font-size"></p>



<p class="has-medium-font-size"><strong>Happy Coding </strong>!!</p>



<p></p>



<p></p>



<hr>



<p class=""></p>



<p class="has-background" style="background-color:#b6d9ac;font-size:18px"><br>Please <strong><em>bookmark </em></strong>this page and <em><strong>share </strong></em>it with your friends.                                                    Please <a href="https://www.thecodebuzz.com/subscription/" target="_blank" rel="noreferrer noopener"><em><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-luminous-vivid-orange-color"><strong>Subscribe</strong> </mark></em></a>to the blog to receive notifications on freshly published (2025) best practices and guidelines for software design and development.</p>




<br>



<hr>



<p class=""></p>



<p></p>



<p></p><p>The post <a href="https://thecodebuzz.com/sqlbulkcopy-bulk-data-insert-quick-efficient-csharp-net/">SqlBulkCopy – Quick and Efficient Bulk Data Insert</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://thecodebuzz.com/sqlbulkcopy-bulk-data-insert-quick-efficient-csharp-net/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>How to Check Which Version of C# App is using</title>
		<link>https://thecodebuzz.com/how-to-check-the-version-of-csharp-windows-mac-linux/</link>
					<comments>https://thecodebuzz.com/how-to-check-the-version-of-csharp-windows-mac-linux/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Sat, 28 Oct 2023 01:28:14 +0000</pubDate>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<guid isPermaLink="false">https://www.thecodebuzz.com/?p=28598</guid>

					<description><![CDATA[<p>How to Check the Version of C# Today in this article, we shall see how to check which version of C# your Application is using. We will see various easy-to-use techniques to verify the version. The below techniques can be used to verify the version on your developer machines or build a machine or cloud [&#8230;]</p>
<p>The post <a href="https://thecodebuzz.com/how-to-check-the-version-of-csharp-windows-mac-linux/">How to Check Which Version of C# App is using</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></description>
										<content:encoded><![CDATA[<h1 class="wp-block-heading"><strong>How to Check the Version of C# </strong></h1>



<figure class="wp-block-image"><img loading="lazy" decoding="async" width="1024" height="397" src="https://www.thecodebuzz.com/wp-content/uploads/2021/11/which-version-of-c-sharp-used-application-1024x397.jpg" alt="which-version-of-c-sharp-using" class="wp-image-19445" srcset="https://thecodebuzz.com/wp-content/uploads/2021/11/which-version-of-c-sharp-used-application-1024x397.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2021/11/which-version-of-c-sharp-used-application-300x116.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2021/11/which-version-of-c-sharp-used-application-768x298.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2021/11/which-version-of-c-sharp-used-application-1536x596.jpg 1536w, https://thecodebuzz.com/wp-content/uploads/2021/11/which-version-of-c-sharp-used-application-785x304.jpg 785w, https://thecodebuzz.com/wp-content/uploads/2021/11/which-version-of-c-sharp-used-application.jpg 1547w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>



<p>Today in this article, we shall see how to check which version of C# your Application is using.</p>



<p><br>We will see various easy-to-use techniques to verify the version. The below techniques can be used to verify the version on your developer machines or build a machine or cloud server.</p>



<p></p>



<p>You may find the need to check the C# version on the hosted application or server or in a cloud container etc.</p>



<p></p>



<p>We will cover the below aspects in the article,</p>



<p></p>



<div class="wp-block-aioseo-table-of-contents"><ul><li><a href="#aioseo-get-c-version-using-command-prompt">Get C# Version using Command line (Windows/Linux/MacOS)</a><ul><li><a href="#aioseo-command">Command</a></li></ul></li><li><a href="#aioseo-get-c-version-using-error-directive">Get C# Version using #Error Directive</a><ul><li><a href="#aioseo-command">Command</a></li></ul></li><li><a href="#aioseo-check-net-core-version--sdk-and-runtime-version-programmatically">Check .NET Core version- SDK and Runtime version Programmatically</a><ul><li><a href="#aioseo-command">Command</a></li></ul></li><li><a href="#aioseo-c-version-using-visual-studio---option-is-disabled">C# Version using Visual Studio &#8211; Option is disabled now</a></li><li><a href="#aioseo-check-all-supported-c-version-till-date">Check all supported C# versions to date</a></li><li><a href="#aioseo-how-to-target-specific-version">How to Target specific version</a></li></ul></div>



<p></p>



<p>Please note that as per recent guidelines from Microsoft default version of C#  used by the application will be driven by the underlying .NET framework used (This is true for .NET or .NET Core framework used).</p>



<p></p>



<p></p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-c-version-using-visual-studio---option-is-disabled"></h2>



<p></p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-get-c-version-using-command-prompt">Get C# Version using Command line (Windows/Linux/MacOS)</h2>



<p></p>



<p>Using any Shell or CLI ex. <strong><em>Visual Studio </em></strong>Command prompt:</p>



<p></p>



<h3 class="wp-block-heading" id="aioseo-command">Command</h3>



<p></p>



<pre class="wp-block-preformatted has-medium-font-size"><strong>csc /langversion:?</strong>
</pre>



<figure class="wp-block-image"><a href="https://www.thecodebuzz.com/how-to-get-a-version-of-c-sharp-language-build-server-cloud" target="_blank" rel="noopener"><img loading="lazy" decoding="async" width="784" height="492" src="https://www.thecodebuzz.com/wp-content/uploads/2021/11/check-c-language-used-on-server.jpg" alt="which-c-sharp-version-net-core-uses get a version of the C# language" class="wp-image-19446" srcset="https://thecodebuzz.com/wp-content/uploads/2021/11/check-c-language-used-on-server.jpg 784w, https://thecodebuzz.com/wp-content/uploads/2021/11/check-c-language-used-on-server-300x188.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2021/11/check-c-language-used-on-server-768x482.jpg 768w" sizes="auto, (max-width: 784px) 100vw, 784px" /></a></figure>



<p></p>



<p>C# compiler determines a default language version based on your project&#8217;s target framework.  The target project when built gets the highest compatible language version by default as per this versioning semantics. </p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-get-c-version-using-error-directive">Get C# Version using #Error Directive</h2>



<p></p>



<p>One can use the below directive to verify the c# language version.<em><strong> </strong></em></p>



<p></p>



<p><em><strong>Just type the below command anywhere in .cs file and mouse hover on it </strong></em></p>



<p></p>



<h3 class="wp-block-heading" id="aioseo-command">Command</h3>



<p></p>



<pre class="wp-block-preformatted has-medium-font-size"><strong>#error version</strong><br><br></pre>



<figure class="wp-block-image size-large"><a href="https://www.thecodebuzz.com/how-to-get-a-version-of-c-sharp-language-build-server-cloud" target="_blank" rel="noopener"><img loading="lazy" decoding="async" width="1024" height="319" src="https://www.thecodebuzz.com/wp-content/uploads/2021/11/which-version-of-c-sharp-used-application-using-cs-file-1024x319.jpg" alt="what-are-the-correct-version-numbers-for-c-sharp" class="wp-image-19447" srcset="https://thecodebuzz.com/wp-content/uploads/2021/11/which-version-of-c-sharp-used-application-using-cs-file-1024x319.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2021/11/which-version-of-c-sharp-used-application-using-cs-file-300x94.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2021/11/which-version-of-c-sharp-used-application-using-cs-file-768x240.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2021/11/which-version-of-c-sharp-used-application-using-cs-file-785x245.jpg 785w, https://thecodebuzz.com/wp-content/uploads/2021/11/which-version-of-c-sharp-used-application-using-cs-file.jpg 1375w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></figure>



<p></p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-check-net-core-version--sdk-and-runtime-version-programmatically">Check .NET Core version- SDK and Runtime version Programmatically</h2>



<p></p>



<p>Additionally, if you are interested in knowing what version of .NET Core or framework is installed please follow the techniques discussed below,</p>



<p></p>



<h3 class="wp-block-heading" id="aioseo-command">Command</h3>



<p></p>



<pre class="wp-block-preformatted">dotnet --list-sdks</pre>



<p></p>



<p>For more details on the commands, please visit the below article,</p>



<p></p>



<ul class="wp-block-list">
<li><a href="https://www.thecodebuzz.com/get-sdk-and-runtime-version-net-core/" target="_blank" rel="noopener" title="How to Check .NET Core version installed SDK and Runtime"><em><strong>How to Check .NET Core version installed SDK and Runtime</strong></em></a></li>
</ul>



<p></p>



<h2 class="wp-block-heading" id="aioseo-c-version-using-visual-studio---option-is-disabled">C# Version using Visual Studio &#8211; Option is disabled now</h2>



<p></p>



<p>Previously it used to be easy to find C# vesion by navigating the Project- Properties-&gt; Build tab.</p>



<p></p>



<p>However as per new guidelines (explained below).. The visual studio above option is disabled.</p>



<p></p>



<p><strong>As per Microsoft guidelines </strong></p>



<p></p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p>Moving forward, however, each version of each framework will have a <strong>single supported </strong>and <strong>default</strong> version, and we won’t support arbitrary versions. To reflect this change in support, this commit permanently disables the language version combo box and adds a link to a document explaining the change.</p>
</blockquote>



<p></p>



<p><em>So if using .NET or .NET Core then the framework will always target only a single language version and which will also be a default version. </em></p>



<p></p>



<p>The default version will be a <strong>major </strong>version of C#.</p>



<p></p>



<p>So with the above guidelines Visual Studio option to check or update the C# language version is disabled.</p>



<p></p>



<p><strong><em>Project > Properties > Build > Advanced </em></strong>option is <strong><em>disabled</em></strong>.</p>



<p></p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-check-all-supported-c-version-till-date">Check all supported C# versions to date </h2>



<p></p>



<p>Please visit the below Microsoft list,</p>



<p></p>



<p><a href="https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/configure-language-version#c-language-version-reference" target="_blank" rel="noopener" title="">https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/configure-language-version#c-language-version-reference</a></p>



<p></p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-how-to-target-specific-version">How to Target specific version </h2>



<p></p>



<p>If you need to change or target your application to use a particular C# <a href="https://www.thecodebuzz.com/modifier-not-valid-this-item-c-use-language-version-greater/">language version</a>, then there are various approaches to achieve this. Please refer to the below article, </p>



<p></p>



<ul class="wp-block-list">
<li><strong><em><a href="https://www.thecodebuzz.com/enable-c-sharp-language-version-guidelines/" target="_blank" rel="noreferrer noopener" title="How to Enable C# Language Versioning – Guidelines">How to Enable C# Language Versioning – Guidelines</a></em></strong></li>
</ul>



<p></p>



<p></p>



<hr>



<p class=""></p>



<p class="has-background" style="background-color:#b6d9ac;font-size:18px"><br>Please <strong><em>bookmark </em></strong>this page and <em><strong>share </strong></em>it with your friends.                                                    Please <a href="https://www.thecodebuzz.com/subscription/" target="_blank" rel="noreferrer noopener"><em><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-luminous-vivid-orange-color"><strong>Subscribe</strong> </mark></em></a>to the blog to receive notifications on freshly published (2025) best practices and guidelines for software design and development.</p>




<br>



<hr>



<p class=""></p>



<p></p>



<p></p>



<p></p><p>The post <a href="https://thecodebuzz.com/how-to-check-the-version-of-csharp-windows-mac-linux/">How to Check Which Version of C# App is using</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://thecodebuzz.com/how-to-check-the-version-of-csharp-windows-mac-linux/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Insert Data Table into SQL Server database &#8211; Best Practices</title>
		<link>https://thecodebuzz.com/insert-data-table-into-sql-server-database/</link>
					<comments>https://thecodebuzz.com/insert-data-table-into-sql-server-database/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Thu, 26 Oct 2023 00:25:08 +0000</pubDate>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Insert Data Table into SQL]]></category>
		<guid isPermaLink="false">https://www.thecodebuzz.com/?p=28633</guid>

					<description><![CDATA[<p>Insert DataTable into SQL Server database table Today in this article we will see a simple approach to Insert Data Table into SQL Server databases Efficiently and Faster way. You may want to insert single or multiple data tables efficiently into an SQL Database. Inserting data from the Datatable can be optimized using SQL Server&#8217;s [&#8230;]</p>
<p>The post <a href="https://thecodebuzz.com/insert-data-table-into-sql-server-database/">Insert Data Table into SQL Server database – Best Practices</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></description>
										<content:encoded><![CDATA[<h1 class="wp-block-heading"><strong>Insert DataTable into SQL Server database table</strong></h1>



<figure class="wp-block-image size-full is-resized"><img loading="lazy" decoding="async" width="723" height="519" src="https://www.thecodebuzz.com/wp-content/uploads/2023/10/DataTable-to-SQL-Server-Database-faster.jpg" alt="insert DataTable into SQL Server " class="wp-image-28637" style="aspect-ratio:1.393063583815029;width:581px;height:auto" srcset="https://thecodebuzz.com/wp-content/uploads/2023/10/DataTable-to-SQL-Server-Database-faster.jpg 723w, https://thecodebuzz.com/wp-content/uploads/2023/10/DataTable-to-SQL-Server-Database-faster-300x215.jpg 300w" sizes="auto, (max-width: 723px) 100vw, 723px" /></figure>



<p>Today in this article we will see a simple approach to Insert Data Table into SQL Server databases Efficiently and Faster way.</p>



<p></p>



<p>You may want to insert single or multiple data tables efficiently into an SQL Database.</p>



<p></p>



<p>Inserting data from the Datatable can be optimized using SQL Server&#8217;s Table-Valued Parameters (TVPs) combined with a custom user-defined table type for each DataTable.</p>



<p></p>



<p></p>



<p>However, if you are interested in inserting the Datatable records without using <strong><em>Table-Valued Parameters </em></strong>(TVPs) or stored procedures(SP) then it can be achieved using <em><strong>parameterized SQL</strong></em> queries which are explained below in detail.</p>



<p></p>



<div class="wp-block-aioseo-table-of-contents"><ul><li><a href="#aioseo-insert-datatable-into-sql-using-table-valued-parameters-tvps">Insert DataTable into SQL  &#8211; Using Table Valued Parameters (TVPs)</a></li><li><a href="#aioseo-insert-datatable-into-sql-without-tvp-or-store-procedure">Insert DataTable into SQL &#8211; Without TVP or Store Procedure (SqlBulkCopy)</a></li><li><a href="#aioseo-asynchronous-sqlbulkcopy-for-bulk-insert">Synchronous SqlBulkCopy for Bulk insert</a></li></ul></div>



<p></p>



<p>Please install <em><strong>System.Data.SqlClient </strong></em>Nuget package as explained in this article to use ADO.NET features in your application where class SqlClient is defined.</p>



<p></p>



<ul class="wp-block-list">
<li><a href="https://www.thecodebuzz.com/connect-sql-server-database-insert-update-delete-sqlclient-csharp/" target="_blank" rel="noopener" title="Connect SQL Server Database C# Example">Connect SQL Server Database C# Example</a></li>
</ul>



<p></p>



<h2 class="wp-block-heading" id="aioseo-insert-datatable-into-sql-using-table-valued-parameters-tvps">Insert DataTable into SQL  &#8211; Using Table Valued Parameters (TVPs)</h2>



<p></p>



<p>For each of your DataTables, create a user-defined table type in SQL. Please make sure the table type matches the structure of the source DataTables. </p>



<p></p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p>This is the most <a href="https://www.thecodebuzz.com/sqlbulkcopy-bulk-data-insert-quick-efficient-csharp-net/">efficient way of inserting</a> the Table into the SQL server Database.</p>
</blockquote>



<p></p>



<p>Please use SQL Server Management Studio or SQL scripts to achieve the same. (Ensure to have process edit access to your DB).</p>



<p></p>



<p><strong>Example </strong></p>



<p></p>



<pre class="wp-block-preformatted">CREATE TYPE EmployeeTypeA AS TABLE
(
        ID INT,
        NAME VARCHAR(25),
        CITY VARCHAR(50),
	COUNTRY VARCHAR(50)
);



CREATE TYPE EmployeeTypeB AS TABLE
(
        ID INT,
        NAME VARCHAR(25),
	CITY VARCHAR(50),
	COUNTRY VARCHAR(50)

);
</pre>



<p>Sample code to execute ADO.NET core for two or multiple tables using the TVP approach.</p>



<p></p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1024" height="645" src="https://www.thecodebuzz.com/wp-content/uploads/2023/10/DataTable-to-SQL-Server-Database-faster-using-able-Valued-Parameters-1024x645.jpg" alt="" class="wp-image-28639" srcset="https://thecodebuzz.com/wp-content/uploads/2023/10/DataTable-to-SQL-Server-Database-faster-using-able-Valued-Parameters-1024x645.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2023/10/DataTable-to-SQL-Server-Database-faster-using-able-Valued-Parameters-300x189.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2023/10/DataTable-to-SQL-Server-Database-faster-using-able-Valued-Parameters-768x484.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2023/10/DataTable-to-SQL-Server-Database-faster-using-able-Valued-Parameters-1536x968.jpg 1536w, https://thecodebuzz.com/wp-content/uploads/2023/10/DataTable-to-SQL-Server-Database-faster-using-able-Valued-Parameters.jpg 1904w, https://thecodebuzz.com/wp-content/uploads/2023/10/DataTable-to-SQL-Server-Database-faster-using-able-Valued-Parameters-785x495.jpg 785w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>



<p></p>



<p>In the above code, we performed the steps in the below order.</p>



<p></p>



<ol class="wp-block-list">
<li>We have two table <code>employeeTableA</code> and <code>employeeTableB</code>, that match the structures of <code>EmployeeTypeA</code> and <code>EmployeeTypeB</code>.</li>



<li>We establish a SqlConnection to your SQL Server.</li>



<li>We create two SqlCommands that call stored procedures, one for each table type, which use TVPs for insertion. These stored procedures should insert data from the TVPs into the respective target tables.</li>



<li>For each SqlCommand, we create a SqlParameter for the TVP, specifying the parameter name, SqlDbType (<code>SqlDbType.Structured</code>), and the TVP&#8217;s type name.</li>



<li>We execute the stored procedures for both <code>EmployeeTypeA</code> and <code>EmployeeTypeB</code>, which will insert the data from the DataTables into the respective tables.</li>
</ol>



<p></p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-insert-datatable-into-sql-without-tvp-or-store-procedure">Insert DataTable into SQL &#8211; Without TVP or Store Procedure (SqlBulkCopy)</h2>



<p></p>



<p>If you are looking for other options like Inserting DataTable into SQL  &#8211; Without TVP or Store Procedure then there is a simple approach of using <strong><em>SqlBulkCopy</em></strong>.</p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-asynchronous-sqlbulkcopy-for-bulk-insert">Synchronous SqlBulkCopy for Bulk insert</h2>



<p></p>



<p>Here is a complete sample code for the same schema discussed above,</p>



<p></p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1024" height="534" src="https://www.thecodebuzz.com/wp-content/uploads/2023/10/DataTable-to-SQL-Server-Database-faster-using-SqlBulkCopy-faster-SQL-data-insert-1-1024x534.jpg" alt="insert-a-data-table-into-sql-server-database-table-SqlBulkCopy" class="wp-image-28642" srcset="https://thecodebuzz.com/wp-content/uploads/2023/10/DataTable-to-SQL-Server-Database-faster-using-SqlBulkCopy-faster-SQL-data-insert-1-1024x534.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2023/10/DataTable-to-SQL-Server-Database-faster-using-SqlBulkCopy-faster-SQL-data-insert-1-300x156.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2023/10/DataTable-to-SQL-Server-Database-faster-using-SqlBulkCopy-faster-SQL-data-insert-1-768x400.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2023/10/DataTable-to-SQL-Server-Database-faster-using-SqlBulkCopy-faster-SQL-data-insert-1-1536x800.jpg 1536w, https://thecodebuzz.com/wp-content/uploads/2023/10/DataTable-to-SQL-Server-Database-faster-using-SqlBulkCopy-faster-SQL-data-insert-1-2048x1067.jpg 2048w, https://thecodebuzz.com/wp-content/uploads/2023/10/DataTable-to-SQL-Server-Database-faster-using-SqlBulkCopy-faster-SQL-data-insert-1-785x409.jpg 785w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>



<p></p>



<p>In the above code, we performed the steps in the below order.</p>



<p></p>



<ol class="wp-block-list">
<li>We have two source tables <code>employeeTableA</code> and <code>employeeTableB</code> which needs to be inserted into SQL</li>



<li>We establish a SqlConnection to your SQL Server.</li>



<li>We consider using SQL transactions to ensure that either all or none of the data from the DataTables is inserted. If everything goes well, then only you commit the transaction.</li>



<li>For each DataTable, you create parameterized SQL queries and use <strong><a href="https://learn.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlbulkcopy?view=dotnet-plat-ext-7.0" target="_blank" rel="noopener" title="">SqlBulkCopy </a></strong>to insert the data.</li>



<li>Since using transaction, commit it to make the changes as final commit.</li>



<li>Close the SqlConnection when you&#8217;re done.</li>
</ol>



<p></p>



<p>Additionally, to handle any exception please make sure to us<strong>e try-and-catch blocks</strong>,</p>



<pre class="wp-block-preformatted">                    try
                    {
                      
                        bulkCopy.WriteToServer(<code>employeeTableA</code> );
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                    finally
                    {
                       
                        connection.Close();
                    }</pre>



<p></p>



<p>While using <strong><em>SqlBulkCopy</em></strong> it&#8217;s <strong>recommended </strong>to use SQL transactions to ensure that either all or none of the data from the DataTables is inserted. </p>



<p></p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p>This approach is reasonably efficient and one of other best option along with TRV.</p>
</blockquote>



<p></p>



<p></p>



<p style="font-size:18px">Do you have any <strong>comments or ideas or any better </strong>suggestions to share?</p>



<p class="has-small-font-size"></p>



<p style="font-size:18px">Please sound off your comments below.</p>



<p class="has-medium-font-size"></p>



<p class="has-medium-font-size"><strong>Happy Coding </strong>!!</p>



<p></p>



<hr>



<p class=""></p>



<p class="has-background" style="background-color:#b6d9ac;font-size:18px"><br>Please <strong><em>bookmark </em></strong>this page and <em><strong>share </strong></em>it with your friends.                                                    Please <a href="https://www.thecodebuzz.com/subscription/" target="_blank" rel="noreferrer noopener"><em><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-luminous-vivid-orange-color"><strong>Subscribe</strong> </mark></em></a>to the blog to receive notifications on freshly published (2025) best practices and guidelines for software design and development.</p>




<br>



<hr>



<p class=""></p>



<p></p>



<p></p><p>The post <a href="https://thecodebuzz.com/insert-data-table-into-sql-server-database/">Insert Data Table into SQL Server database – Best Practices</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://thecodebuzz.com/insert-data-table-into-sql-server-database/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>C# Read file as Key value Dictionary pair</title>
		<link>https://thecodebuzz.com/c-read-file-as-key-value-dictionary-pair/</link>
					<comments>https://thecodebuzz.com/c-read-file-as-key-value-dictionary-pair/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Sun, 22 Oct 2023 18:17:21 +0000</pubDate>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[csharp-file-as-key-value-dictionary]]></category>
		<category><![CDATA[csharp-read-file-as-key-value-dictionary-pair-net]]></category>
		<guid isPermaLink="false">https://www.thecodebuzz.com/?p=28584</guid>

					<description><![CDATA[<p>C# Read file as Key value Dictionary pair Today in this article, we will see how to Read files as Key value Dictionary pairs using a simple and effective approach. Using this approach you can generate a Dictionary list of possible values. To keep it simple we will use the below file where we have [&#8230;]</p>
<p>The post <a href="https://thecodebuzz.com/c-read-file-as-key-value-dictionary-pair/">C# Read file as Key value Dictionary pair</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></description>
										<content:encoded><![CDATA[<h1 class="wp-block-heading">C# Read file as Key value Dictionary pair</h1>



<p>Today in this article, we will see how to Read files as Key value Dictionary pairs using a simple and effective approach.</p>



<p></p>



<p>Using this approach you can generate a Dictionary list of possible values.</p>



<p></p>



<p>To keep it simple we will use the below file where we have content separated by<strong> pipe delimiter</strong>.</p>



<p></p>



<p>However, the below-discussed approach can be used for other separator like comma-separated values(,) or space( ) or any other separator, etc.</p>



<p>  </p>



<p></p>



<p><strong>Sample file </strong></p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1024" height="368" src="https://www.thecodebuzz.com/wp-content/uploads/2023/10/c-sharp-dictionary-how-to-read-key-value-from-file-1024x368.jpg" alt="" class="wp-image-28585" srcset="https://thecodebuzz.com/wp-content/uploads/2023/10/c-sharp-dictionary-how-to-read-key-value-from-file-1024x368.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2023/10/c-sharp-dictionary-how-to-read-key-value-from-file-300x108.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2023/10/c-sharp-dictionary-how-to-read-key-value-from-file-768x276.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2023/10/c-sharp-dictionary-how-to-read-key-value-from-file-1536x552.jpg 1536w, https://thecodebuzz.com/wp-content/uploads/2023/10/c-sharp-dictionary-how-to-read-key-value-from-file-785x282.jpg 785w, https://thecodebuzz.com/wp-content/uploads/2023/10/c-sharp-dictionary-how-to-read-key-value-from-file.jpg 1802w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>



<p></p>



<p></p>



<p>Below is a sample code of how we can read the content and create a <strong>dictionary </strong>of key values pair using Employee ID as Key and Employee Name as Value.</p>



<p></p>



<p>Below fileLineReader is each line from the target file. It read the content and split the data using the &#8220;I&#8221; pipe delimiter. </p>



<p></p>



<h2 class="wp-block-heading">File with Pipe Delimiter</h2>



<p></p>



<pre class="wp-block-preformatted">                   string[] keyvalue = fileLineReader.Current.Split('|');
                    if (keyvalue.Length == 3)
                    {
                        employeeDict.Add(keyvalue[1], keyvalue[0]);
                    }</pre>



<p></p>



<p>Below sample code using File Emuerator </p>



<p></p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1024" height="508" src="https://www.thecodebuzz.com/wp-content/uploads/2023/10/Csharp-Dictionary-how-to-read-keyvalue-from-file-1024x508.jpg" alt="C# Read file as Key value Dictionary " class="wp-image-28586" srcset="https://thecodebuzz.com/wp-content/uploads/2023/10/Csharp-Dictionary-how-to-read-keyvalue-from-file-1024x508.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2023/10/Csharp-Dictionary-how-to-read-keyvalue-from-file-300x149.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2023/10/Csharp-Dictionary-how-to-read-keyvalue-from-file-768x381.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2023/10/Csharp-Dictionary-how-to-read-keyvalue-from-file-1536x761.jpg 1536w, https://thecodebuzz.com/wp-content/uploads/2023/10/Csharp-Dictionary-how-to-read-keyvalue-from-file-2048x1015.jpg 2048w, https://thecodebuzz.com/wp-content/uploads/2023/10/Csharp-Dictionary-how-to-read-keyvalue-from-file-785x389.jpg 785w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /><figcaption class="wp-element-caption">#image_title #separator_sa #post_seo_title</figcaption></figure>



<p></p>



<h2 class="wp-block-heading">Parse File line with Space</h2>



<p></p>



<pre class="wp-block-preformatted">string[] keyvalue = fileLineReader.Current.Split(' ');
                    if (keyvalue.Length == 3)
                    {
                        employeeDict.Add(keyvalue[1], keyvalue[0]);
                    }</pre>



<p></p>



<h2 class="wp-block-heading">Parse File line with comma-separated values</h2>



<p></p>



<pre class="wp-block-preformatted">string[] keyvalue = fileLineReader.Current.Split(' ');
                    if (keyvalue.Length == 3)
                    {
                        employeeDict.Add(keyvalue[1], keyvalue[0]);
                    }</pre>



<p></p>



<p></p>



<p></p>



<p>Are you dealing with a huge file that needs chunking logic to be used due to its size? </p>



<p></p>



<p>If you have a huge file size to deal with then it&#8217;s important to check the performance of the filter or query.</p>



<p> Kindly check below 2-3 approaches discussed below for effective chunking and converting the file into a dictionary.</p>



<p></p>



<p></p>



<p>Kindly visit below a few useful articles on dealing with file size, </p>



<p></p>



<ul class="wp-block-list">
<li> <a href="https://www.thecodebuzz.com/read-large-file-into-a-byte-array-with-chunks-csharp/" title="Read a Large File in Chunks in C# Guidelines">Read a Large File in Chunks in C# Guidelines</a>?</li>
</ul>



<p></p>



<p></p>



<p style="font-size:18px">Do you have any <strong>comments or ideas or any better </strong>suggestions to share?</p>



<p class="has-small-font-size"></p>



<p style="font-size:18px">Please sound off your comments below.</p>



<p class="has-medium-font-size"></p>



<p class="has-medium-font-size"><strong>Happy Coding </strong>!!</p>



<p></p>



<p></p>



<hr>



<p class=""></p>



<p class="has-background" style="background-color:#b6d9ac;font-size:18px"><br>Please <strong><em>bookmark </em></strong>this page and <em><strong>share </strong></em>it with your friends.                                                    Please <a href="https://www.thecodebuzz.com/subscription/" target="_blank" rel="noreferrer noopener"><em><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-luminous-vivid-orange-color"><strong>Subscribe</strong> </mark></em></a>to the blog to receive notifications on freshly published (2025) best practices and guidelines for software design and development.</p>




<br>



<hr>



<p class=""></p>



<p></p>



<p></p>



<p></p><p>The post <a href="https://thecodebuzz.com/c-read-file-as-key-value-dictionary-pair/">C# Read file as Key value Dictionary pair</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://thecodebuzz.com/c-read-file-as-key-value-dictionary-pair/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
