Scenario: Download Script
You are working as C# developer, you need to write a program that can read file's information such as- FolderPath
- FileName
- LastWriteTime
- CreateTime
- FileSizeinKB
from a table and write into SQL Server table. Also as part of file information , you would like to insert folder from which we are reading the file properties.
Step 1:
First of all you need to create a table in SQL Server database in which you would like to insert file information.
CREATE TABLE [dbo].[FileInformation]( id int identity(1,1), FolderPath VARCHAR(255), FileName VARCHAR(255), [LastWriteTime] DateTime, [CreateTime] Datetime, FileSizeinKB Int)
Step 2:
Create new Project and then choose Console Application and use below script.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using System.Data.SqlClient; namespace _01_WriteFileProperitestoTable { class Program { static void Main(string[] args) { //Declare Variable and set value,Provide the folder which contains files string VarDirectoryPath = "C:\\Source\\"; //Create Connection to SQL Server SqlConnection SQLConnection = new SqlConnection(); SQLConnection.ConnectionString= "Data Source = (local); Initial Catalog =TechBrothersIT; "
+ "Integrated Security=true;"; //get all files from directory or folder to get file's information string[] files = Directory.GetFiles(VarDirectoryPath); SqlCommand SqlCmd = new SqlCommand(); SqlCmd.Connection = SQLConnection; SQLConnection.Open(); //loop through files foreach (string filename in files) { FileInfo file = new FileInfo(filename); SqlCmd.CommandText = "Insert into dbo.FileInformation("; SqlCmd.CommandText += "[FolderPath],[FileName],[LastWriteTime],[CreateTime],[FileSizeinKB])"; SqlCmd.CommandText +=" Values('" + VarDirectoryPath + "','" + file.Name + "','" + file.LastWriteTime + "','" + file.CreationTime + "','" + file.Length / 1024 + "')"; SqlCmd.ExecuteNonQuery(); } SQLConnection.Close(); } } }
Save the script and then execute. It should read the file properties from the folder you have given and insert into SQL Server Table.
How to read file's properties in C# and insert into SQL Server Table - C# Tutorial
Such a great post! It looks like a blog and in this content was useful for freshers. Keeping a good job.
ReplyDeleteTableau Training in Chennai
Tableau Course in Chennai
Pega Training in Chennai
Excel Training in Chennai
Power BI Training in Chennai
Oracle Training in Chennai
Unix Training in Chennai
Tableau Training in Chennai
Tableau Course in Chennai
This is really a nice and informative, containing all information and also has a great impact on the new technology. Thanks for sharing it, Affinity at serangoon showroom
ReplyDeleteI really like your take on the issue. I now have a clear idea on what this matter is all about.. buy instagram likes uk instant
ReplyDeletei really like this article please keep it up. gouna villas
ReplyDeleteGreat job for publishing such a beneficial web site. Your web log isn’t only useful but it is additionally really creative too. There tend to be not many people who can certainly write not so simple posts that artistically. Continue the nice writing gouna villas
ReplyDeleteI found your this post while searching for some related information on blog search...Its a good post..keep posting and update the information. offplan projects for sale in Dubai
ReplyDeleteWith land being an unstable industry, you should invest in a great deal of energy to know the suitable planning and market rates prior to selling your property. Homes for sale in Huntington Hills
ReplyDeleteNext we tackle the property the board costs. Sell property Ireland
ReplyDelete