Quantcast
Channel: Welcome To TechBrothersIT
Viewing all 1878 articles
Browse latest View live

How to recover databases which are in Restore status on SQL Server Instance - SQL Server Tutorial

$
0
0

Scenario:

Think about a scenario when you were restoring bunch of databases and by mistake you has with NORecovery in script or You were restoring a database and you thought that you have to restore transaction log on top of it and now you don't need to restore Transaction log but need to bring database online from Restoring State.

How to bring databases online from Restoring State in SQL Server

You can use below script to bring database from Restoring State to Online.

RESTOREDATABASE YourDatabaseName WITH RECOVERY

If you have multiple databases in Restore state and you want to bring all of them online, you can use below script. Also you can filter the databases in Select statement to include or exclude databases you want to bring online from Restoring state.

USE MASTER
GO

DECLARE @DatabaseName ASVARCHAR(128)

DECLARE Cur CURSOR
FOR
--Get list of Databases You want to Bring online from Restore State
SELECT NAME
FROM sys.databases
WHERE database_id > 4
AND state_desc = 'RESTORING'

OPEN Cur

FETCHNEXT
FROM Cur
INTO @DatabaseName

WHILE@@FETCH_STATUS = 0
BEGIN
DECLARE @SQLBringOnline NVARCHAR(500) = NULL

SET @SQLBringOnline = 'RESTORE DATABASE "' + @DatabaseName + '" WITH RECOVERY'

PRINT @SQLBringOnline

EXEC (@SQLBringOnline)

FETCHNEXT
FROM Cur
INTO @DatabaseName
END

CLOSE Cur

DEALLOCATE Cur

I executed above script on my SQL Server Instance and it brought all databases online which were in Restore State on SQL Server Instance.
How to change Database state from Restore to Online in SQL Server


Bring databases online from Restore State in SQL Server - SQL Server Tutorial



C# - How to Import multiple text files to SQL Server Table by using C Sharp

$
0
0

Scenario: Download Script

You are working as C# developer and you need to write a program that should be able to read text files from a folder and load to SQL Server Table. 

How to load multiple text files to SQL Server table by using C#

The below code can be used to load multiple flat files or text files to load to SQL Server table. You can change the variable values to handle different situations. Let's say if you need to load .CSV files, you can change the FileExtension value =".CSV".

If you are interested to load pipe delimited files instead of comma, you can change value for filedelimiter="|" and rest of the program should work fine to load pipe delimited files.


Create Table statement.

CREATETABLE dbo.TechBrothersITCourse (
id INT
,CourseName VARCHAR(100)
)

I created Console Application by using below C# code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Data.SqlClient;

namespace TechBrothersIT.com_CSharp_Tutorial
{
class Program
{
staticvoid Main(string[] args)
{

try
{

//Declare Variable and set values
//provide input folder
string SourceFolder = @"C:\Source\";
//provide the extension of files you would like to read
string FileExtension = "
.txt";
//provide the table name in which you would like to load data
string TableName = "
dbo.TechBrothersITCourse";
//provide the file delimiter such as comma,pipe
string filedelimiter = "
,";
//provide the Archive folder where you would like to move file
string ArchiveFodler = @"
C:\Archive\";

//Create Connection to SQL Server
SqlConnection SQLConnection = new SqlConnection();
SQLConnection.ConnectionString = "
Data Source = (local); Initial Catalog =TechBrothersIT; "
+ "
Integrated Security=true;";

// get files from folder according to file extension
string[] fileEntries = Directory.GetFiles(SourceFolder, "
*" + FileExtension);
foreach (string fileName in fileEntries)
{
System.IO.StreamReader SourceFile =
new System.IO.StreamReader(fileName);

string line = "
";
Int32 counter = 0;

SQLConnection.Open();
while ((line = SourceFile.ReadLine()) != null)
{
//skip the header row
if (counter > 0)
{
//prepare insert query
string query = "
Insert into " + TableName +
"
Values ('" + line.Replace(filedelimiter, "','") + "')";

//execute sqlcommand to insert record
SqlCommand myCommand = new SqlCommand(query, SQLConnection);
myCommand.ExecuteNonQuery();
}
counter++;
}

SourceFile.Close();
SQLConnection.Close();

//Move the file to Archive folder, fileName variable contains complete path for input file.
//Path.GetFileName() to get only the file name from full path
File.Move(fileName, ArchiveFodler + Path.GetFileName(fileName));

}
}
catch (IOException Exception)
{
Console.Write(Exception);
}
}

}
}


Save the script and run the program.It should read the files from source folder, load to sql server table and move to archive folder. 
C# - How to Import multiple Text files to SQL Server Table by using C Sharp



How to Import or load all the picture/image files from a folder to SQL Server Table by using TSQL- SQL Server Tutorial

$
0
0

Scenario:

You are working as SQL Server Developer for Insurance Company. They get the claimed signed letters and scan and place in a folder. You need to write the SQL Script that should be able to read those claim files and load into SQL Server table from a folder.

here are my sample files, you can have any type of files such as pdf, bmp,jpg etc.
How to Import Picture files from a folder to SQL Server Table



Let's go ahead and create a table first in which we would like to load the files from a folder.

USE YourDBName
GO
CREATETABLE dbo.MyPictures
(
PictureId INTIDENTITYPRIMARYKEY,
PictureFileName VARCHAR(100),
PictureData VARBINARY(MAX),
LoadedDateTime DATETIME
)

The below script can be used to load all the files from a folder to above table.

IF OBJECT_ID('tempdb..#FileList') ISNOTNULL
DROPTABLE #FileList


--Folder path where files are present
Declare @SourceFolder VARCHAR(100)
SET @SourceFolder='C:\Source\'

CREATETABLE #FileList (
Id intidentity(1,1),
FileName nvarchar(255),
Depthsmallint,
FileFlag bit)

--Load the file names from a folder to a table
INSERTINTO #FileList (FileName,Depth,FileFlag)
EXEC xp_dirtree @SourceFolder, 10, 1

--Use Cursor to loop throught backups files
--Select * From #FileList
Declare @FileName VARCHAR(500)

DECLARE Cur CURSORFOR
SELECT FileName from #FileList
where fileflag=1

OPEN Cur
FETCHNextFROM Cur INTO @FileName
WHILE@@FETCH_STATUS = 0
BEGIN

DECLARE @InsertSQL NVARCHAR(MAX)=NULL
--Prepare SQL Statement for insert
SET @InsertSQL=
'INSERT INTO dbo.MyPictures(PictureFileName, LoadedDateTime,PictureData)
SELECT '
''+@FileName+''',getdate(),BulkColumn
FROM Openrowset( Bulk '
''+@SourceFolder+@FileName+''', Single_Blob) as Image'

--Print and Execute SQL Insert Statement to load file
Print @InsertSQL
EXEC(@InsertSQL)

FETCHNextFROM Cur INTO @FileName
END
CLOSE Cur
DEALLOCATE Cur

Let's check the table if all files are loaded successfully from a folder to table.

Select * from dbo.MyPictures

Import files from a folder to SQL Server Table by using T-SQL 






How to Import or load Multiple XML files to SQL Server Table - SQL Server Tutorial

$
0
0

Scenario:

You are working as SQL Server Developer or ETL developer or DBA and you need to load multiple xml files from a folder to SQL Server Table. There are multiple ways to achieve this

You can use SQL Server Integration Services and create an SSIS Package to load the files from a folder
Or
You can use use TSQL to load the xml files, In this post we are going to use TSQL to load the XML files from a folder to SQL server Table.

How to Import XML files to SQL Server Table from a folder



Step 1: 
Let's create a table with a column of XML data types in which we want to load XML Files from a folder.

USE YourDBName
GO
CREATETABLE dbo.XMLFilesTable
(
Id INTIDENTITYPRIMARYKEY,
FileName VARCHAR(100),
XMLData XML,
LoadedDateTime DATETIME
)

Step 2:
Use below script, Change the folder path in which your XML files are present. The script should load all the xml files from given folder to table.

IF OBJECT_ID('tempdb..#FileList') ISNOTNULL
DROPTABLE #FileList


--Folder path where files are present
Declare @SourceFolder VARCHAR(100)
SET @SourceFolder='C:\Source\'

CREATETABLE #FileList (
Id intidentity(1,1),
FileName nvarchar(255),
Depthsmallint,
FileFlag bit)

--Load the file names from a folder to a table
INSERTINTO #FileList (FileName,Depth,FileFlag)
EXEC xp_dirtree @SourceFolder, 10, 1

--Use Cursor to loop throught files
--Select * From #FileList
Declare @FileName VARCHAR(500)

DECLARE Cur CURSORFOR
SELECT FileName from #FileList
where fileflag=1

OPEN Cur
FETCHNextFROM Cur INTO @FileName
WHILE@@FETCH_STATUS = 0
BEGIN

DECLARE @InsertSQL NVARCHAR(MAX)=NULL
--Prepare SQL Statement for insert
SET @InsertSQL=
'INSERT INTO dbo.XMLFilesTable(FileName, LoadedDateTime,XMLData)
SELECT '
''+@FileName+''',getdate(),Convert(XML,BulkColumn ) As BulkColumn
FROM Openrowset( Bulk '
''+@SourceFolder+@FileName+''', Single_Blob) as Image'


--Print and Execute SQL Insert Statement to load file
Print @InsertSQL
EXEC(@InsertSQL)

FETCHNextFROM Cur INTO @FileName
END
CLOSE Cur
DEALLOCATE Cur


Step 3: 
Query the table to take a look if all XML files loaded correctly to SQL Server table.
How to Load XML files a folder to SQL Server Table by TSQL - SQL Server Tutorial

C# Scripts

$
0
0
C# Scripts


  1. C# - How to get file Properties and insert into SQL Server table by using C Sharp
  2. C# - Delete files from a folder those are older than some number of days by using C Sharp
  3. C# - Why console window disappear immediately without display my output in C Sharp?
  4. C# - What is the difference between Console.Write and Console.WriteLine Methods in C Sharp
  5. C# - How to get Oldest file from a folder by using C Sharp
  6. C# - How to get the latest file from a folder by using C Sharp
  7. C# - How to copy all files in a folder to another folder in C Sharp
  8. C# - How to copy files and overwriting existing files if exists in a folder by using C Sharp
  9. C# - How to Move files from a folder to Another Folder in C Sharp
  10. C# - File.MoveTo Does not work - Cannot create a file when that file already exists. - C Sharp
  11. C# - How to move and rename files to another folder by adding datetime to file name by C Sharp
  12. C# - How to Copy files from a folder to another folder and add date-time to file names in C Sharp
  13. C# - How to Import or Load Text file to SQL Server Table by using C Sharp
  14. C# - How to Import multiple text files to SQL Server Table by using C Sharp
  15. C# - Importing Files to SQL Server Table with Dynamic Columns by using C Sharp
  16. C# - How to create SQL Server Table dynamically from Flat file and Load data to Table in C Sharp
  17. C# - How to Load data to SQL Server Tables from flat files according to file names by using C Sharp
  18. C# - How to Export Data from SQL Server Table or View to Text File in C Sharp
  19. C# - How to Export Stored Procedure Results to Text File in C Sharp
  20. C# - How to Import Multiple CSV Files to SQL Server from a Folder in C Sharp
  21. C# - How to write multiple CSV files to Single CVS File in C Sharp
  22. C# - How to Export all the tables from a database to flat files by using C Sharp


C# - Importing Files to SQL Server Table with Dynamic Columns by using C Sharp

$
0
0

Scenario : Download Script

You are working as C# developer, you get multiple text of csv files from different clients those you need to load to dbo.Customer table. But there is little problem. Each client will not send exact number of columns what we have for you table.

Createtable dbo.Customer(
Id INT,
Name VARCHAR(100),
Dob Date)
 
Think about that client A sends always file with id, name and dob columns.
Client B can send file with Id and Name. Also Client B can send some files with three columns id,name and dob.
Client C can also send the file with only column Name and Dob or any other combination.

One thing to noticed here, the files can have same columns or less but will never have more columns than SQL Server table.

The below script can be used to load multiple files to SQL server table with same or less columns what we have in our SQL Server Table. By changing values for variables such as file delimiter you can use this to load pipe delimiter files. By changing value for fileExtension variable to .csv , you can load csv files.

sample files

How to Load Flat files to SQL Server Table with Dynamic Columns in File by using C Sharp




using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Data.SqlClient;

namespace TechBrothersIT.com_CSharp_Tutorial
{
class Program
{
staticvoid Main(string[] args)
{

string datetime = DateTime.Now.ToString("yyyyMMddHHmmss");
string LogFolder = @"C:\Log\";
try
{

//Declare Variables and provide values
string SourceFolderPath = @"
C:\Source\";
string FileExtension = "
.txt";
string FileDelimiter = "
,";
string TableName = "
dbo.Customer";
string ArchiveFolder = @"
C:\Archive\";


//Get files from folder
string[] fileEntries = Directory.GetFiles(SourceFolderPath, "
*" + FileExtension);
foreach (string fileName in fileEntries)
{

//Create Connection to SQL Server
SqlConnection SQLConnection = new SqlConnection();
SQLConnection.ConnectionString = "
Data Source = (local); Initial Catalog =TechBrothersIT; "
+ "
Integrated Security=true;";

int counter = 0;
string line;
string ColumnList = "
";

System.IO.StreamReader SourceFile =
new System.IO.StreamReader(fileName);

SQLConnection.Open();
while ((line = SourceFile.ReadLine()) != null)
{
if (counter == 0)
{
//By using Header Row, Build Column List
ColumnList = "
[" + line.Replace(FileDelimiter, "],[") + "]";

}
else
{

//Build and Execute Insert Statement to insert record
string query = "
Insert into " + TableName + " (" + ColumnList + ") ";
query += "
VALUES('" + line.Replace(FileDelimiter, "','") + "')";

SqlCommand SQLCmd = new SqlCommand(query, SQLConnection);
SQLCmd.ExecuteNonQuery();
}

counter++;
}

SourceFile.Close();
SQLConnection.Close();
//move the file to archive folder after adding datetime to it
File.Move(fileName, ArchiveFolder +
(fileName.Replace(SourceFolderPath, "
")).Replace(FileExtension, "")
+ "
_" + datetime + FileExtension);

}
}
catch (Exception exception)
{
// Create Log File for Errors
using (StreamWriter sw = File.CreateText(LogFolder
+ "
\\" + "ErrorLog_" + datetime + ".log"))
{
sw.WriteLine(exception.ToString());

}

}

}
}
}
I execute above script , it read the reads from multiple files, Loaded them to SQL Server
table and then moved the files to archive folder successfully.




Here are records loaded by package in dbo.Customer table from my sample files.



C# - How to create SQL Server Table dynamically from Flat file and Load data to Table in C Sharp

$
0
0

Scenario : Download Script

You are working as C# developer, You received flat files or text files or csv files in one of the source folder. You need to write C# program that should read the file columns and create table and load the data from file. Once data is loading move the file to archive folder.
The table will be created with name of file. If already exists, we would like to drop the table and created.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Data.SqlClient;

namespace TechBrothersIT.com_CSharp_Tutorial
{
class Program
{
staticvoid Main(string[] args)
{

string datetime = DateTime.Now.ToString("yyyyMMddHHmmss");
string LogFolder = @"C:\Log\";
try
{

//Declare Variables and provide values
string SourceFolderPath = @"
C:\Source\";
string FileExtension = "
.txt";
string FileDelimiter = "
,";
string ArchiveFolder = @"
C:\Archive\";
string ColumnsDataType = "
NVARCHAR(4000)";
string SchemaName = "
dbo";


//Get files from folder
string[] fileEntries = Directory.GetFiles(SourceFolderPath, "
*" + FileExtension);
foreach (string fileName in fileEntries)
{

//Create Connection to SQL Server in which you would like to create tables and load data
SqlConnection SQLConnection = new SqlConnection();
SQLConnection.ConnectionString = "
Data Source = (local); Initial Catalog =TechBrothersIT; "
+ "
Integrated Security=true;";

//Writing Data of File Into Table
string TableName = "
";
int counter = 0;
string line;
string ColumnList = "
";

System.IO.StreamReader SourceFile =
new System.IO.StreamReader(fileName);

SQLConnection.Open();
while ((line = SourceFile.ReadLine()) != null)
{
if (counter == 0)
{

//Read the header and prepare Create Table Statement
ColumnList = "
[" + line.Replace(FileDelimiter, "],[") + "]";
TableName = (((fileName.Replace(SourceFolderPath, "
")).Replace(FileExtension, "")).Replace("\\", ""));
string CreateTableStatement = "
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[" + SchemaName + "].";
CreateTableStatement += "[" + TableName + "]'
)";
CreateTableStatement += "
AND type in (N'U'))DROP TABLE [" + SchemaName + "].";
CreateTableStatement += "
[" + TableName + "] Create Table " + SchemaName + ".[" + TableName + "]";
CreateTableStatement += "
([" + line.Replace(FileDelimiter, "] " + ColumnsDataType + ",[") + "] " + ColumnsDataType + ")";
SqlCommand CreateTableCmd = new SqlCommand(CreateTableStatement, SQLConnection);
CreateTableCmd.ExecuteNonQuery();

}
else
{

//Prepare Insert Statement and execute to insert data
string query = "
Insert into " + SchemaName + ".[" + TableName + "] (" + ColumnList + ") ";
query += "
VALUES('" + line.Replace(FileDelimiter, "','") + "')";

SqlCommand SQLCmd = new SqlCommand(query, SQLConnection);
SQLCmd.ExecuteNonQuery();
}

counter++;
}

SourceFile.Close();
SQLConnection.Close();
//move the file to archive folder after adding datetime to it
File.Move(fileName, ArchiveFolder + "
\\" + (fileName.Replace(SourceFolderPath, "")).Replace(FileExtension, "") + "_" + datetime + FileExtension);

}
}
catch (Exception exception)
{
// Create Log File for Errors
using (StreamWriter sw = File.CreateText(LogFolder
+ "
\\" + "ErrorLog_" + datetime + ".log"))
{
sw.WriteLine(exception.ToString());

}

}

}
}
}

I executed the script and it created the tables for all the flat files and loaded the data. Files were moved successfully to archive folder after loading to SQL Server table.

C# - How to Load data to SQL Server Tables from flat files according to file names by using C Sharp

$
0
0

Scenario : Download Script

You are working as C# developer for insurance company. You get multiple files on daily basis in your Source folder. The files comes with date and some of them comes with date and time. You need to extract Name from file and then load to table accordingly. Each file Name does match with table Name.



How to load flat files to SQL Server Tables according to the name of file by using C sharp



Here is script that I used to create tables for above sample files.

Createtable dbo.Customer
(Id int,
Name varchar(100)
)
GO

CreateTable dbo.TechBrothersInfo
(
Id int,
CourseTitle VARCHAR(100)

)

The program should be able to load any number of files as long as the tables exist and file name contains table name as part of full name.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Data.SqlClient;

namespace TechBrothersIT.com_CSharp_Tutorial
{
class Program
{
staticvoid Main(string[] args)
{

string datetime = DateTime.Now.ToString("yyyyMMddHHmmss");
string LogFolder = @"C:\Log\";
try
{

//Declare Variables and provide values
string SourceFolderPath = @"
C:\Source\";
string FileExtension = "
.txt";
string FileDelimiter = "
,";
string ArchiveFolder = @"
C:\Archive\";
string SchemaName = "
dbo";


//Create Connection to SQL Server in which you like to load files
SqlConnection SQLConnection = new SqlConnection();
SQLConnection.ConnectionString = "
Data Source = (local); Initial Catalog =TechBrothersIT; "
+ "
Integrated Security=true;";

//Reading file names one by one
string SourceDirectory = SourceFolderPath;
string[] fileEntries = Directory.GetFiles(SourceDirectory, "
*" + FileExtension);
foreach (string fileName in fileEntries)
{

string TableName = "
";
//Remove All Numbers and other characters and leave alphabets for name
System.Text.RegularExpressions.Regex rgx = new System.Text.RegularExpressions.Regex("
[^a-zA-Z]");
TableName = rgx.Replace(fileName.Replace(SourceDirectory, "
").Replace(FileExtension, ""), "");

//Writing Data of File Into Table
int counter = 0;
string line;
string ColumnList = "
";

System.IO.StreamReader SourceFile =
new System.IO.StreamReader(fileName);
SQLConnection.Open();
while ((line = SourceFile.ReadLine()) != null)
{
if (counter == 0)
{
ColumnList = "
[" + line.Replace(FileDelimiter, "],[") + "]";

}
else
{
string query = "
Insert into " + SchemaName + "." + TableName + " (" + ColumnList + ") ";
query += "
VALUES('" + line.Replace(FileDelimiter, "','") + "')";

SqlCommand SQLCmd = new SqlCommand(query, SQLConnection);
SQLCmd.ExecuteNonQuery();
}

counter++;
}
SourceFile.Close();
SQLConnection.Close();

//move the file to archive folder after adding datetime to it
File.Move(fileName, ArchiveFolder + "
\\" +
(fileName.Replace(SourceFolderPath, "
")).Replace(FileExtension, "")
+ "
_" + datetime + FileExtension);

}
}
catch (Exception exception)
{
// Create Log File for Errors
using (StreamWriter sw = File.CreateText(LogFolder
+ "
\\" + "ErrorLog_" + datetime + ".log"))
{
sw.WriteLine(exception.ToString());

}

}

}
}
}



Save the program and execute.After loading the files , the files should be moved to archive folder. I tested and it load the files data successfully to my tables as shown below.

How to load csv/ txt files to SQL Server Tables according to name of files by using C#





C# - How to Export Data from SQL Server Table or View to Text File in C Sharp

$
0
0

Scenario: Download Script

You are working as C# developer. You are asked to write a program that should read the data from a table, view or function and write the results to flat file. Each time you run the program it should get the data from table or view and create flat file with date-time.


The below program will create flat file on each execution with results extracted from a table or view.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Data.SqlClient;
using System.Data;


namespace TechBrothersIT.com_CSharp_Tutorial
{
class Program
{
staticvoid Main(string[] args)
{

string datetime = DateTime.Now.ToString("yyyyMMddHHmmss");
string LogFolder = @"C:\Log\";
try
{

//Declare Variables and provide values
string FileNamePart = "
Customer";//Datetime will be added to it
string DestinationFolder = @"
C:\Destination\";
string TableName = "
Dbo.Customer";
string FileDelimiter = "
,"; //You can provide comma or pipe or whatever you like
string FileExtension = "
.txt"; //Provide the extension you like such as .txt or .csv


//Create Connection to SQL Server in which you like to load files
SqlConnection SQLConnection = new SqlConnection();
SQLConnection.ConnectionString = "
Data Source = (local); Initial Catalog =TechBrothersIT; "
+ "
Integrated Security=true;";

//Read data from table or view to data table
string query = "
Select * From " + TableName;
SqlCommand cmd = new SqlCommand(query, SQLConnection);
SQLConnection.Open();
DataTable d_table = new DataTable();
d_table.Load(cmd.ExecuteReader());
SQLConnection.Close();

//Prepare the file path
string FileFullPath = DestinationFolder + "
\\" + FileNamePart + "_" + datetime + FileExtension;

StreamWriter sw = null;
sw = new StreamWriter(FileFullPath, false);

// Write the Header Row to File
int ColumnCount = d_table.Columns.Count;
for (int ic = 0; ic < ColumnCount; ic++)
{
sw.Write(d_table.Columns[ic]);
if (ic < ColumnCount - 1)
{
sw.Write(FileDelimiter);
}
}
sw.Write(sw.NewLine);

// Write All Rows to the File
foreach (DataRow dr in d_table.Rows)
{
for (int ir = 0; ir < ColumnCount; ir++)
{
if (!Convert.IsDBNull(dr[ir]))
{
sw.Write(dr[ir].ToString());
}
if (ir < ColumnCount - 1)
{
sw.Write(FileDelimiter);
}
}
sw.Write(sw.NewLine);

}

sw.Close();

}
catch (Exception exception)
{
// Create Log File for Errors
using (StreamWriter sw = File.CreateText(LogFolder
+ "
\\" + "ErrorLog_" + datetime + ".log"))
{
sw.WriteLine(exception.ToString());

}

}

}
}
}

I executed the program couple of times and Customer file was created as shown below.
How to create Text file from SQL Server Table or View in C#


C# - How to Export Stored Procedure Results to Text File in C Sharp

$
0
0

Scenario : Download Script

You are working as C# developer, You need to developer a program that should export the results of a Stored Procedure to Text file. Every time the program executes, it should run the Stored Procedure and export the results to new flat file. The file should be create with date-time.


Let's create sample table and Stored Procedure first.

Createtable dbo.Customer(
Id INT,
Name VARCHAR(100),
Dob Date)

insertinto dbo.Customer values(1,'John','1980-01-02')
go

Createprocedure dbo.prc_Customer
AS
BEGIN
Select * From dbo.Customer
END


The below C# will be used to export the Stored Procedure results to text file.
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Data.SqlClient;
using System.Data;


namespace TechBrothersIT.com_CSharp_Tutorial
{
class Program
{
staticvoid Main(string[] args)
{

string datetime = DateTime.Now.ToString("yyyyMMddHHmmss");
string LogFolder = @"C:\Log\";
try
{

//Declare Variables and provide values
string FileNamePart = "
Customer";//Datetime will be added to it
string DestinationFolder = @"
C:\Destination\";
string StoredProcedureName = "
dbo.prc_Customer";//Provide SP name,you Can provide with Parameter if you like
string FileDelimiter = "
,"; //You can provide comma or pipe or whatever you like
string FileExtension = "
.txt"; //Provide the extension you like such as .txt or .csv


//Create Connection to SQL Server in which you like to load files
SqlConnection SQLConnection = new SqlConnection();
SQLConnection.ConnectionString = "
Data Source = (local); Initial Catalog =TechBrothersIT; "
+ "
Integrated Security=true;";

//Execute Stored Procedure and save results in data table
string query = "
EXEC " + StoredProcedureName;
SqlCommand cmd = new SqlCommand(query, SQLConnection);
SQLConnection.Open();
DataTable d_table = new DataTable();
d_table.Load(cmd.ExecuteReader());
SQLConnection.Close();

//Prepare the file path
string FileFullPath = DestinationFolder + "
\\" + FileNamePart + "_" + datetime + FileExtension;

StreamWriter sw = null;
sw = new StreamWriter(FileFullPath, false);

// Write the Header Row to File
int ColumnCount = d_table.Columns.Count;
for (int ic = 0; ic < ColumnCount; ic++)
{
sw.Write(d_table.Columns[ic]);
if (ic < ColumnCount - 1)
{
sw.Write(FileDelimiter);
}
}
sw.Write(sw.NewLine);

// Write All Rows to the File
foreach (DataRow dr in d_table.Rows)
{
for (int ir = 0; ir < ColumnCount; ir++)
{
if (!Convert.IsDBNull(dr[ir]))
{
sw.Write(dr[ir].ToString());
}
if (ir < ColumnCount - 1)
{
sw.Write(FileDelimiter);
}
}
sw.Write(sw.NewLine);

}

sw.Close();

}
catch (Exception exception)
{
// Create Log File for Errors
using (StreamWriter sw = File.CreateText(LogFolder
+ "
\\" + "ErrorLog_" + datetime + ".log"))
{
sw.WriteLine(exception.ToString());

}

}

}
}
}

I executed the script couple of times and it generated the flat files from Stored Procedure results. The files were created with date-time.
How to Export Stored Procedure Results to Text file in C#

C# - How to Import Multiple CSV Files to SQL Server from a Folder in C Sharp

$
0
0

Scenario : Download Script

You are working as C# developer, You get tons of files in a folder. All the files has the same definition.You need to load all the files to SQL server Table. 

Let's say here is SQL Server Table in which we need to load the files

Createtable dbo.Customer(
Id INT,
Name VARCHAR(100),
Dob Date)

Sample files for this demo shown below
How to load multiple files to SQL Server table in C#

The below program will load all the files from a folder to SQL Server table.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Data.SqlClient;
using System.Data;


namespace TechBrothersIT.com_CSharp_Tutorial
{
class Program
{
staticvoid Main(string[] args)
{

string datetime = DateTime.Now.ToString("yyyyMMddHHmmss");
string LogFolder = @"C:\Log\";
try
{

//Declare Variables and provide values
string SourceFolderPath = @"
C:\Source\"; //Provide the Source Folder where files are present
string FileExtension = "
.txt"; //Provide the extension of files you need to load, can be .txt or .csv
string FileDelimiter = "
,"; // provide the file delimiter such as comma or pipe
string ArchiveFolder = @"
C:\Archive\"; //Provide the archive folder path where files will be moved
string TableName = "
dbo.Customer"; //Provide the table name in which you would like to load the files.


//Create Connection to SQL Server in which you like to load files
SqlConnection SQLConnection = new SqlConnection();
SQLConnection.ConnectionString = "
Data Source = (local); Initial Catalog =TechBrothersIT; "
+ "
Integrated Security=true;";

//Reading file names one by one
string[] fileEntries = Directory.GetFiles(SourceFolderPath, "
*" + FileExtension);
foreach (string fileName in fileEntries)
{

//Writing Data of File Into Table
int counter = 0;
string line;
string ColumnList = "
";

System.IO.StreamReader SourceFile =
new System.IO.StreamReader(fileName);
SQLConnection.Open();
while ((line = SourceFile.ReadLine()) != null)
{
if (counter == 0)
{
//By using Header Row, Build Column List
ColumnList = "
[" + line.Replace(FileDelimiter, "],[") + "]";

}
else
{

//Build and Execute Insert Statement to insert record
string query = "
Insert into " + TableName + " (" + ColumnList + ") ";
query += "
VALUES('" + line.Replace(FileDelimiter, "','") + "')";

SqlCommand SQLCmd = new SqlCommand(query, SQLConnection);
SQLCmd.ExecuteNonQuery();
}

counter++;
}

SourceFile.Close();
SQLConnection.Close();
//move the file to archive folder after adding datetime to it
File.Move(fileName, ArchiveFolder + "
\\" +
(fileName.Replace(SourceFolderPath, "
")).Replace(FileExtension, "")
+ "
_" + datetime + FileExtension);
}
}
catch (Exception exception)
{
// Create Log File for Errors
using (StreamWriter sw = File.CreateText(LogFolder
+ "
\\" + "ErrorLog_" + datetime + ".log"))
{
sw.WriteLine(exception.ToString());

}

}

}
}
}

Go ahead and execute your script. It should load all the files from a folder to SQL Server table that name you have provided in variable.
How to load all the files from a folder to SQL Server Table in C#


C# - How to write multiple CSV files to Single CVS File in C Sharp

$
0
0

Scenario: Download Script

You are working as C# developer, you need to write a program that should read multiple csv files from a folder and write to single csv file. All the input files has the same number of columns.

Sample files from Source folder as shown below.
How to write multiple flat files to single flat file in C#

The below C# code can be used to write multiple files to single file.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Data.SqlClient;
using System.Data;


namespace TechBrothersIT.com_CSharp_Tutorial
{
class Program
{
staticvoid Main(string[] args)
{

string datetime = DateTime.Now.ToString("yyyyMMddHHmmss");
string LogFolder = @"C:\Log\";
try
{

//Declare Variables and provide values
string DestinationFolder = @"
C:\Destination\"; //Provide Destination folder path
string FileDelimiter = "
,"; //Provide file delimiter such as comma or pipe for source files
string FileExtension = "
.txt"; //Provide file extension such as .txt or .csv for source file
string SourceFolder = @"
C:\Source\"; //Provide the source folder where your input files are present
string FileName = "
Customer_Final"; //Provide the file name in which you like to write all files
string DestinationFileExtension = "
.txt"; //Provide Extension for destination file

//Building Destination file name
string FileFullPath = DestinationFolder + "
\\" + FileName + "_" + datetime + DestinationFileExtension;

int counter = 0;

//Looping through the flat files
string[] fileEntries = Directory.GetFiles(SourceFolder, "
*" + FileExtension);
foreach (string fileName in fileEntries)
{
string line;
System.IO.StreamReader SourceFile =
new System.IO.StreamReader(fileName);

StreamWriter sw = null;
sw = new StreamWriter(FileFullPath, true);

int linecnt = 0;
while ((line = SourceFile.ReadLine()) != null)
{
//Write only the header from first file
if (counter == 0 && linecnt == 0)
{
sw.Write(line);
sw.Write(sw.NewLine);

}
//Write data records from flat files
if (linecnt != 0)
{
sw.Write(line);
sw.Write(sw.NewLine);
}
linecnt++;
counter++;
}

sw.Close();

}

}
catch (Exception exception)
{
// Create Log File for Errors
using (StreamWriter sw = File.CreateText(LogFolder
+ "
\\" + "ErrorLog_" + datetime + ".log"))
{
sw.WriteLine(exception.ToString());

}

}

}
}
}


Once I executed above script, it read all the input files and wrote to single csv file as shown below.
How to write multiple CSV files to Single CSV File in C#

C# - How to Export all the tables from a database to flat files by using C Sharp

$
0
0

Scenario : Download Script

You are working as C# developer, You need to create an C# program that should create flat files for all the tables from a database dynamically. There should be one file for each table and it should be created with date-time.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Data.SqlClient;
using System.Data;


namespace TechBrothersIT.com_CSharp_Tutorial {
class Program {
staticvoid Main(string[] args) {

string datetime = DateTime.Now.ToString("yyyyMMddHHmmss");
string LogFolder = @ "C:\Log\";
try {

//Declare Variables and provide values
string DestinationFolder = @ "
C:\Destination\"; //Provide the destination folder
string FileDelimiter = "
,"; //Provide the file delimiter such as comma or pipe
string FileExtension = "
.txt"; //Provide the extension such as .txt or .csv


//Create Connection to SQL Server
SqlConnection SQLConnection = new SqlConnection();
SQLConnection.ConnectionString = "
Data Source = (local); Initial Catalog =TechBrothersIT; " + "Integrated Security=true;";

//Read list of Tables with Schema from Database
string query = "
SELECT Schema_name(schema_id) AS SchemaName,name AS TableName FROM sys.tables WHERE is_ms_shipped = 0";


SqlCommand cmd = new SqlCommand(query, SQLConnection);
SQLConnection.Open();
DataTable dt = new DataTable();
dt.Load(cmd.ExecuteReader());

//Loop through datatable(dt) that has schema and table names
foreach(DataRow dt_row in dt.Rows) {
string SchemaName = "
";
string TableName = "
";
object[] array = dt_row.ItemArray;
SchemaName = array[0].ToString();
TableName = array[1].ToString();

string FileFullPath = DestinationFolder + "
\\" + SchemaName + "_" + TableName + "_" + datetime + FileExtension;

//Get the data for a table into data table
string data_query = "
Select * From [" + SchemaName + "].[" + TableName + "]";
SqlCommand data_cmd = new SqlCommand(data_query, SQLConnection);
DataTable d_table = new DataTable();
d_table.Load(data_cmd.ExecuteReader());

StreamWriter sw = null;
sw = new StreamWriter(FileFullPath, false);

// Write the Header Row to File
int ColumnCount = d_table.Columns.Count;
for (int ic = 0; ic < ColumnCount; ic++) {
sw.Write(d_table.Columns[ic]);
if (ic < ColumnCount - 1) {
sw.Write(FileDelimiter);
}
}
sw.Write(sw.NewLine);

// Write All Rows to the File
foreach(DataRow dr in d_table.Rows) {
for (int ir = 0; ir < ColumnCount; ir++) {
if (!Convert.IsDBNull(dr[ir])) {
sw.Write(dr[ir].ToString());
}
if (ir < ColumnCount - 1) {
sw.Write(FileDelimiter);
}
}
sw.Write(sw.NewLine);

}

sw.Close();

}
SQLConnection.Close();

} catch (Exception exception) {
// Create Log File for Errors
using(StreamWriter sw = File.CreateText(LogFolder + "
\\" + "ErrorLog_" + datetime + ".log")) {
sw.WriteLine(exception.ToString());

}

}

}
}
}


I executed above code and it export all the tables from SQL Server Database to text files.
How to Export all the tables form SQL Server Database to Flat files ( CSV Files) by using C#
 

C# - How to read Excel Cell Value in C#

$
0
0

Scenario: Download Script

You are working as C# developer, You need to write a program that should be able to read Excel Cell value and save into variable. Once saved into a variable then you can use for different purposes in your program.


How to read Excel Cell Value in C# from Excel Sheet


Below Script can be used to read any cell value. You simple have to provide the value to CellToRead variable. Let's say if I would like to read E7 Cell. I will use below C# script to read E7 Excel Cell. you can change to any cell you like to read.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Data.OleDb;
using System.Data;


namespace TechBrothersIT.com_CSharp_Tutorial
{
class Program
{
staticvoid Main(string[] args)
{

string datetime = DateTime.Now.ToString("yyyyMMddHHmmss");
string LogFolder = @"C:\Log\";
try
{

string CellToRead = "E
7"; //Provide the Excel Cell you want to read
string CellName = "
";
Int32 CellNumber;
string fileFullPath = @"
C:\Source\TechBrothersit.xlsx"; //Provide Excel File Path
string sheetname="
Sheet1$"; //Provide the Sheet Name
string CellValue="
";

var alphanumericstring = new System.Text.RegularExpressions.Regex("
(?<Alpha>[a-zA-Z]*)(?<Numeric>[0-9]*)");
var match = alphanumericstring.Match(CellToRead);

CellName = match.Groups["
Alpha"].Value;
int.TryParse(match.Groups["
Numeric"].Value, out CellNumber);

//Create Excel Connection
string ConStr;
string HDR;
HDR = "
YES";
ConStr = "
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
fileFullPath + "
;Extended Properties=\"Excel 12.0;HDR=" +
HDR + ";IMEX=0\"";

OleDbConnection cnn = new OleDbConnection(ConStr);
cnn.Open();
DataTable dtSheet = cnn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);


//Read the Excel Cell value from Excel Sheet
OleDbCommand oconn = new OleDbCommand("select * from ["
                                            sheetname + CellName + 
                                            (CellNumber - 1).ToString() 
                                            + ":" + CellToRead + "]", cnn);
OleDbDataAdapter adp = new OleDbDataAdapter(oconn);
DataTable dt = new DataTable();
adp.Fill(dt);
cnn.Close();

foreach (DataRow row in dt.Rows)
{
object[] array = row.ItemArray;
CellValue = array[0].ToString();
}

//Print Excel Cell Value
Console.WriteLine("CelltoRead :" + CellToRead + " Cell Value:" + CellValue);
Console.ReadLine();

}

catch (Exception exception)
{
// Create Log File for Errors
using (StreamWriter sw = File.CreateText(LogFolder
+ "\\" + "ErrorLog_" + datetime + ".log"))
{
sw.WriteLine(exception.ToString());

}

}

}
}
}
I executed above script and it print the Excel cell value for me. The value is saved in CellValue variable and you are free to use anywhere you like in your program.
How to read Excel Cell Value in C# Program

C# - How to Export All tables of a database to Excel Files with Date-time in C#

$
0
0

Scenario : Download Script

You are working as C# developer, you need to write a program that should export all the tables to Excel files. Each Table should be export to separate Excel file and Excel file should have date-time added to it.

Below script can be use to export all the tables from SQL Server Database to Excel files.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Data;


namespace TechBrothersIT.com_CSharp_Tutorial
{
class Program
{
staticvoid Main(string[] args)
{

string datetime = DateTime.Now.ToString("yyyyMMddHHmmss");
string LogFolder = @"C:\Log\";
try
{

string FolderPath = @"
C:\Destination\"; //Provide Destination folder path
//Create Connection to SQL Server
SqlConnection SQLConnection = new SqlConnection();
SQLConnection.ConnectionString = "
Data Source = (local); Initial Catalog =TechBrothersIT; "
+ "
Integrated Security=true;";


//Read list of Tables with Schema from Database to Export to Excel Files
string query = "
SELECT Schema_name(schema_id) AS SchemaName,name AS TableName";
query+="
FROM sys.tables WHERE is_ms_shipped = 0";

//Get Table Names and Schema Names for Export to Excel files
SqlCommand cmd = new SqlCommand(query, SQLConnection);
SQLConnection.Open();
DataTable dt = new DataTable();
dt.Load(cmd.ExecuteReader());
SQLConnection.Close();

//Loop through datatable(dt) that has schema and table names
foreach (DataRow dt_row in dt.Rows)
{
string SchemaName = "
";
string TableName = "
";
object[] array = dt_row.ItemArray;
SchemaName = array[0].ToString();
TableName = array[1].ToString();


string ExcelFileName = "
";
ExcelFileName = SchemaName + "
_" + TableName + "_" + datetime;
OleDbConnection Excel_OLE_Con = new OleDbConnection();
OleDbCommand Excel_OLE_Cmd = new OleDbCommand();

//Construct ConnectionString for Excel
string connstring = "
Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source="
+ FolderPath + ExcelFileName
+ "
;" + "Extended Properties=\"Excel 12.0 Xml;HDR=YES;\"";

//Load Data into DataTable from SQL ServerTable
string queryString =
"SELECT * from " + SchemaName + "." + TableName;
SqlDataAdapter adapter = new SqlDataAdapter(queryString, SQLConnection);
DataSet ds = new DataSet();
adapter.Fill(ds);

//Get Header Columns
string TableColumns = "";

// Get the Column List from Data Table so can create Excel Sheet with Header
foreach (DataTable table in ds.Tables)
{
foreach (DataColumn column in table.Columns)
{
TableColumns += column + "],[";
}
}

// Replace most right comma from Columnlist
TableColumns = ("[" + TableColumns.Replace(",", " Text,").TrimEnd(','));
TableColumns = TableColumns.Remove(TableColumns.Length - 2);
//MessageBox.Show(TableColumns);


//Use OLE DB Connection and Create Excel Sheet
Excel_OLE_Con.ConnectionString = connstring;
Excel_OLE_Con.Open();
Excel_OLE_Cmd.Connection = Excel_OLE_Con;
Excel_OLE_Cmd.CommandText = "Create table [" + SchemaName + "_" +
TableName + "] (" + TableColumns + ")";
Excel_OLE_Cmd.ExecuteNonQuery();


//Write Data to Excel Sheet from DataTable dynamically
foreach (DataTable table in ds.Tables)
{
String sqlCommandInsert = "";
String sqlCommandValue = "";
foreach (DataColumn dataColumn in table.Columns)
{
sqlCommandValue += dataColumn + "],[";
}

sqlCommandValue = "[" + sqlCommandValue.TrimEnd(',');
sqlCommandValue = sqlCommandValue.Remove(sqlCommandValue.Length - 2);
sqlCommandInsert = "INSERT into [" + SchemaName + "_" + TableName
+ "] (" + sqlCommandValue + ") VALUES(";

int columnCount = table.Columns.Count;
foreach (DataRow row in table.Rows)
{
string columnvalues = "";
for (int i = 0; i < columnCount; i++)
{
int index = table.Rows.IndexOf(row);
columnvalues += "'" + table.Rows[index].ItemArray[i] + "',";

}
columnvalues = columnvalues.TrimEnd(',');
var command = sqlCommandInsert + columnvalues + ")";
Excel_OLE_Cmd.CommandText = command;
Excel_OLE_Cmd.ExecuteNonQuery();
}

}
Excel_OLE_Con.Close();
}

}

catch (Exception exception)
{
// Create Log File for Errors
using (StreamWriter sw = File.CreateText(LogFolder
+ "\\" + "ErrorLog_" + datetime + ".log"))
{
sw.WriteLine(exception.ToString());

}

}

}
}
}


I executed above program and it was able to generate Excel files for all the SQL Server tables and load data to them.
How to Export all the SQL Server Tables to Excel Files Dynamically in C#

 

C# - How to Export large table data to multiple Excel Sheets on Single Excel file in C#

$
0
0

Scenario: Download Script

You are working as C# developer, You need to create a C# program that should read a large table from SQL Server database and export to multiple Excel Sheets on Single Excel file. You should be able to make decision how many records you would like to load to each Excel sheet.


The below C# script can be used to Export large table to multiple Excel sheets on Single Excel file. You don't have to worry about having unique column such as id etc. , As we generate row number dynamically for all the records and then divide them to multiple sheets.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Data;


namespace TechBrothersIT.com_CSharp_Tutorial
{
class Program
{
staticvoid Main(string[] args)
{

string datetime = DateTime.Now.ToString("yyyyMMddHHmmss");
string LogFolder = @"C:\Log\";
try
{

string FolderPath = @"
C:\Destination\"; //Provide Destination folder path
string ExcelFileName = "
Customer"; //Provide the Excel File Name you like to create
string TableName = "
dbo.Customer";//Provide the table that you want to export to excel
Int32 RecordCntPerSheet = 100;//Provide the number of rows you want per sheet
string RecordCntPerSheetDecimal = RecordCntPerSheet + "
.0";
ExcelFileName = ExcelFileName + "
_" + datetime;


OleDbConnection Excel_OLE_Con = new OleDbConnection();
OleDbCommand Excel_OLE_Cmd = new OleDbCommand();

//Construct ConnectionString for Excel
string connstring = "
Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source="
+ FolderPath + ExcelFileName
+ "
;" + "Extended Properties=\"Excel 12.0 Xml;HDR=YES;\"";

//drop Excel file if exists
File.Delete(FolderPath + "\\" + ExcelFileName + ".xlsx");


//Create Connection to SQL Server
SqlConnection SQLConnection = new SqlConnection();
SQLConnection.ConnectionString = "
Data Source = (local); Initial Catalog =TechBrothersIT; "
+ "
Integrated Security=true;";


//Read distinct Group Values for each Excel Sheet
string query = "
select ceiling(count(*)/" + RecordCntPerSheetDecimal + ") AS LoopCnt from" + TableName;
decimal LoopCnt = 0;

//Get the Count of Sheets need to be created
SqlCommand cmd = SQLConnection.CreateCommand();
cmd.CommandText = query;
SQLConnection.Open();
LoopCnt = (decimal)cmd.ExecuteScalar();
SQLConnection.Close();


string SheetName = "
Sheet";
int startRowCnt = 0;
int endRowCnt = RecordCntPerSheet;

for (int sheetloop = 1; sheetloop <= LoopCnt; sheetloop++)
{
//Load Data into DataTable from SQL ServerTable
string queryString = "
;with cte as (Select *, Row_Number() over (order by (Select 1)) AS RowNumber from" +
TableName + "
) Select * From cte where RowNumber > " + startRowCnt.ToString() + " and RowNumber<=" + endRowCnt.ToString();
SqlDataAdapter adapter = new SqlDataAdapter(queryString, SQLConnection);
DataSet ds = new DataSet();
adapter.Fill(ds);

//Get Header Columns
string TableColumns = "
";

// Get the Column List from Data Table so can create Excel Sheet with Header
foreach (DataTable table in ds.Tables)
{
foreach (DataColumn column in table.Columns)
{
TableColumns += column + "
],[";
}
}

// Replace most right comma from Columnlist
TableColumns = ("
[" + TableColumns.Replace(",", " Text,").TrimEnd(','));
TableColumns = TableColumns.Remove(TableColumns.Length - 2);

//Use OLE DB Connection and Create Excel Sheet
Excel_OLE_Con.ConnectionString = connstring;
Excel_OLE_Con.Open();
Excel_OLE_Cmd.Connection = Excel_OLE_Con;
Excel_OLE_Cmd.CommandText = "
Create table [" + SheetName
+ sheetloop.ToString() + "
] ("
+ TableColumns + "
)";
Excel_OLE_Cmd.ExecuteNonQuery();


//Write Data to Excel Sheet from DataTable dynamically
foreach (DataTable table in ds.Tables)
{
String sqlCommandInsert = "
";
String sqlCommandValue = "
";
foreach (DataColumn dataColumn in table.Columns)
{
sqlCommandValue += dataColumn + "
],[";
}

sqlCommandValue = "
[" + sqlCommandValue.TrimEnd(',');
sqlCommandValue = sqlCommandValue.Remove(sqlCommandValue.Length - 2);
sqlCommandInsert = "
INSERT into [" + SheetName + sheetloop.ToString()
+ "
] (" + sqlCommandValue + ") VALUES(";

int columnCount = table.Columns.Count;
foreach (DataRow row in table.Rows)
{
string columnvalues = "
";
for (int i = 0; i < columnCount; i++)
{
int index = table.Rows.IndexOf(row);
columnvalues += "
'" + table.Rows[index].ItemArray[i] + "',";

}
columnvalues = columnvalues.TrimEnd(',');
var command = sqlCommandInsert + columnvalues + "
)";
Excel_OLE_Cmd.CommandText = command;
Excel_OLE_Cmd.ExecuteNonQuery();
}

}

startRowCnt += RecordCntPerSheet;
endRowCnt += RecordCntPerSheet;
Excel_OLE_Con.Close();
}

}
catch (Exception exception)
{
// Create Log File for Errors
using (StreamWriter sw = File.CreateText(LogFolder
+ "
\\" + "ErrorLog_" + datetime + ".log"))
{
sw.WriteLine(exception.ToString());

}

}

}
}
}


I executed the above script on for my dbo.Customer table and it was able to export 100 records on each sheet as I have set RecordsPerSheet=100 and export all the records to multiple excel sheets.
How to export data to multiple excel sheets from single table by record count in C#

C# - How to save SQL Query Results to variable in C# ( Scalar value returned by query)

$
0
0

Scenario: Download Script

You are working as C# developer, You are writing C# Program in which you need to execute SQL Query that returns you single value, you need to execute query and save the result into variable.

In below script we will run two queries and save results into integer type and string type variables.

I queried my Customer tale to get total count and Top 1 Name in below example.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Data.SqlClient;
using System.Data;


namespace TechBrothersIT.com_CSharp_Tutorial
{
class Program
{
staticvoid Main(string[] args)
{

string datetime = DateTime.Now.ToString("yyyyMMddHHmmss");
string LogFolder = @"C:\Log\";
try
{

//Create Connection to SQL Server
SqlConnection SQLConnection = new SqlConnection();
SQLConnection.ConnectionString = "
Data Source = (local); Initial Catalog =TechBrothersIT; "
+ "
Integrated Security=true;";


//Query for getting Count
string QueryCnt = "
select count(*) from dbo.Customer";
//Query for getting Name ( Returns only single value)
string QueryName = "
select Top 1 Name from dbo.Customer";



//Execute Queries and save results into variables
SqlCommand CmdCnt = SQLConnection.CreateCommand();
CmdCnt.CommandText = QueryCnt;

SqlCommand CmdName = SQLConnection.CreateCommand();
CmdName.CommandText = QueryName;

SQLConnection.Open();
Int32 CustomerCnt = (Int32)CmdCnt.ExecuteScalar();
string CustomerName = (string)CmdName.ExecuteScalar();
SQLConnection.Close();

//Show values from variables
Console.WriteLine("
CustomerName:" + CustomerName);
Console.WriteLine("
CustomerCount:" + CustomerCnt.ToString());
Console.ReadLine();
}
catch (Exception exception)
{
// Create Log File for Errors
using (StreamWriter sw = File.CreateText(LogFolder
+ "
\\" + "ErrorLog_" + datetime + ".log"))
{
sw.WriteLine(exception.ToString());

}

}

}
}
}

Here is my output from above program.
How to save SQL Query output to Variables in C# ( Scalar value)


How to get Record Count for all the tables in SQL Server Instance in SQL Server - SQL Server Tutorial

$
0
0

Scenario:

You are working as SQL Server DBA or Developer, You need to write script that should get you record count for all the tables in all the database in SQL Server Instances.


Solution:

As we have to get row count for tables in all the databases, we will be using Cursor to loop through database names and get record count for all the tables in each database. We will insert the information in temp table and then finally we will query at the end to see the records. 

USE master
GO

--Drop Temp table if exists to save record count for all the tables on SQL Server Instance
IF OBJECT_ID('tempdb..##RecordCount') ISNOTNULL
DROPTABLE ##RecordCount

CREATETABLE ##RecordCount (
[ServerName] VARCHAR(128)
,[DatabaseName] VARCHAR(128)
,[SchemaName] VARCHAR(128)
,[TableName] VARCHAR(128)
,RecordCnt BIGINT
)

--Use Cursor to Loop through Databases
DECLARE @DatabaseName ASVARCHAR(500)

DECLARE Cur CURSOR
FOR
SELECT NAME
FROM sys.databases
WHERE database_id > 4

OPEN Cur

FETCHNEXT
FROM Cur
INTO @DatabaseName

WHILE@@FETCH_STATUS = 0
BEGIN
DECLARE @DBName AS NVARCHAR(500);

SET @DBName = QUOTENAME(N'' + @DatabaseName + '');

--USE Dynamic SQL to get record count for all tables from each database
EXEC (
N'USE ' + @DBName + N'; EXEC(''
Insert into ##RecordCount
Select @@ServerName AS ServerName,DB_Name() AS DatabaseName,
Schema_name(schema_id) as SchemaName,t.name,s.rows
from sysindexes s
inner join sys.tables t
on s.id=t.object_id
WHERE s.indid IN ( 0, 1, 255 )
AND is_ms_shipped = 0
'
');'
);

FETCHNEXT
FROM Cur
INTO @DatabaseName
END

CLOSE Cur

DEALLOCATE Cur

--Return ServerName,DatabaseName,SchemaName,TableName and Record Count from Temp Table
SELECT *
FROM ##RecordCount


I execute above script on my SQL Server Instance and it returned me Server Name, Database Name, Schema Name, Table Name and Row Count.
How to get Row Count for all the tables in SQL Server Instance - SQL Server Tutorial

C# - How to use Variables into SqlCommand Statement & Insert into database table in C#

$
0
0

Scenario: Download Script

You are working as C# developer, you need to write a program that should write the variable values to SQL Server Table. 

Let's create the sample test table first and then we will save the values in variables and create our insert statement by using variable values and finally execute to insert into SQL table.

CREATETABLE [dbo].[Customer](
[Id] [int] NULL,
[Name] [varchar](100) NULL,
[Dob] [date] NULL
)

The below C# script can be used to insert the variable values to our dbo.Customer table.
 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.IO;


namespace TechBrothersIT.com_CSharp_Tutorial
{
class Program
{
staticvoid Main(string[] args)
{
//the datetime and Log folder will be used for error log file in case error occured
string datetime = DateTime.Now.ToString("yyyyMMddHHmmss");
string LogFolder = @"C:\Log\";
try
{

//Create Connection to SQL Server Database
SqlConnection SQLConnection = new SqlConnection();
SQLConnection.ConnectionString = "
Data Source = (local); Initial Catalog =TechBrothersIT; "
+ "
Integrated Security=true;";

//Declare variables and set values
Int32 id = 1;
string Name = "
Aamir Shahzad";
string dob = "
1982/01/02";

//Prepare Insert Query by using variables
string InsertQuery = "
";
InsertQuery = "
Insert into dbo.Customer(id,name,dob)";
InsertQuery += "
Values(" + id + ",'" + Name + "','" +dob + "')";

//Execute Insert Query to Insert Data into SQL Server Table
SqlCommand cmd = SQLConnection.CreateCommand();
cmd.CommandText = InsertQuery;
SQLConnection.Open();
cmd.ExecuteNonQuery();
SQLConnection.Close();

}
catch (Exception exception)
{
// Create Log File for Errors
using (StreamWriter sw = File.CreateText(LogFolder
+ "
\\" + "ErrorLog_" + datetime + ".log"))
{
sw.WriteLine(exception.ToString());

}

}

}
}
}

Let's check the table after execution and see if the record is inserted successfully. 
How to insert variable values to SQL Server Table in C#

How to Change Recovery Model to Full for all User Databases in SQL Server - SQL Server Scripts

$
0
0

Scenario:

You are working as SQL Server DBA or developer, You need to prepare some script that should change the recovery model for all the user databases to Full recovery model.

The below script can be used to change the recovery model of all user databases to Full. You can also change the select query to include or exclude databases in the list.

USE master 
go 
DECLARE @DBName VARCHAR(200) 
DECLARE db_cursor CURSOR FOR 
  --Make changes to Query to Include or Exclude database 
  SELECT NAME 
  FROM   sys.databases 
  WHERE  NAME NOT IN ( 'master', 'tempdb', 'model', 'msdb' ) 

OPEN db_cursor 

FETCH next FROM db_cursor INTO @DBName 

WHILE @@FETCH_STATUS = 0 
  BEGIN 
      IF ( (SELECT recovery_model_desc 
            FROM   sys.databases 
            WHERE  NAME = @DBName) <> 'FULL' ) 
        BEGIN 
            DECLARE @SQL NVARCHAR(500) 

            SET @SQL='ALTER DATABASE [' + @DBName 
                     + '] SET RECOVERY FULL' 

            PRINT @SQL 

            EXEC ( @SQL) 
        END 

      FETCH next FROM db_cursor INTO @DBName 
  END 

CLOSE db_cursor 

DEALLOCATE db_cursor 

--Check recovery model for all databases 
SELECT NAME AS DBName, 
       recovery_model_desc 
FROM   sys.databases 
Viewing all 1878 articles
Browse latest View live