WordDataMerge
I’ve just completed v.1.0 of “WordDataMerge”, a small utility that takes a DOCX template with data fields ([[field1]], [[field2]]) and a data source (any data source that implements IEnumerable or IListSource) and generates a Word DOCX with one page per record.
Example Template: Template.docx
Output: Output.docx
Class library download + sample code to run the utility:
https://sourceforge.net/projects/worddatamerge/files/
Great Article on Sorting a Generic List in C#
Windows Batch Script – Copy SQL Server Tables Across Servers
Today, I’m explaining how to use a script that can copy an entire SQL Server database from one server to another. This is especially helpful when there is a need to copy production data to a development tier or vice versa. The script can also be customized to copy only a few tables, or even a single table.
The script works like this:
- The syntax to run is:
CopyDatabase server1 server2 myDatabase server1_user server2_user server1_password server2_password
- A “bcp out” command is sent to a SQL Server database to return the data from a table and store the result as a file. I generally use the format “tablename.txt” to store the result in the same folder as the batch script.
Ex:
bcp myDatabase.dbo.myTable out myTable.txt -n -S %1 -U%4 -P%5
- All of the data in the same table is deleted (be sure to backup your data before testing this script!)
sqlcmd -S %2 -U%6 -P%7 -Q "DELETE FROM myDatabase.dbo.myTable" -b
- The data in the local TXT file is sent to the second SQL Server via a “bcp in” command.
Ex:
bcp myDatabase.dbo.myTable in myTable.txt -n -S %2 -U%6 -P%7 -E
The full script is below. Add as many tables as necessary to meet your requirements.
@echo off rem "Usage: CopyDatabase.bat ServerFrom ServerTo DatabaseTo Username Password" @if "%5" == "" goto usage echo Server From : %1 echo Server To : %2 echo Database To : %3 echo FromUsername : %4 echo FromPassword : %5 echo ToUsername : %6 echo ToPassword : %7 </code><code> pause</code> Echo Copying <tablename> from %1 =========================================== bcp <tablename> out <filename> -n -S %1 -U%4 -P%5 rem *** Add more tables here *** rem bcp <tablename2> out <filename2> -n -S %1 -U%4 -P%5 Echo Finished Copying =================================================== Echo Begin DELETING ===================================================== Echo Deleting data from <tablename> on %2 ===================================== sqlcmd -S %2 -U%6 -P%7 -Q "DELETE FROM <tablename>" -b rem *** Add more tables here *** rem sqlcmd -S %2 -U%6 -P%7 -Q "DELETE FROM <tablename2>" -b Echo Finished Deleting ==================================================== Echo Begin Importing ====================================================== Echo Loading <tablename> to %2 ============================================= bcp <tablename> in <filename> -n -S %2 -U%6 -P%7 -E rem *** Add more tables here *** rem <tablename2> in <filename2> -n -S %2 -U%6 -P%7 -E goto end :usage echo Usage: CopyDatabase.bat ServerFrom ServerTo DatabaseTo FromUsername FromPassword ToPassword ToPassword :end echo Finished!