Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

A Little Sqlserver Script Helper

3.55/5 (4 votes)
17 Jan 2007CPOL 1   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:

C#
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:

SQL
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!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)