65.9K
CodeProject is changing. Read more.
Home

A Little Sqlserver Script Helper

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.55/5 (4 votes)

Jan 10, 2007

CPOL
viewsIcon

32991

downloadIcon

394

When you want to get the data script from sqlserver, use this, it can be fun.

Introduction

I found that it is hard to get data script from SQL Server 2005, so I made this little tool to do the job. You can update it in your own style. Have fun!

Step 1: Get the tables within the database.

Step 2: Get the data script by selected table.

Step 3: Copy the data to clipboard.

It's only raw code, more upgrades in the future.

This is GetFields() method to assemble the fields:

private string GetFields(Type t, object Value)
{            
 string strLeft = "'";
 string strRight = "'";
 if (t == typeof(Int32) || t == typeof(int) || t == typeof(Int16) || t == typeof(Int64))
 {
 return Value.ToString();
 }

 if (t == typeof(string))
 {
   return "N" + strLeft + Value.ToString() + strRight;
 }
 return strLeft + Value.ToString() + strRight;
}

To get the tables within the dababase, use this:

SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE'

Updated

  1. Add update script for single and multi table
  2. Add update and insert script for sync between source and destination databases
  3. Add TABLE_CONSTRAINTS

To use this, click More.

Start the main UI

Then click GenAll, by gen script between the 2 databases.

Sample screenshot

You can update it in your own style, then you'll get your script helper!

Thanks for your feedback!