Ankit Agarwal

Ankit Agarwal

  • NA
  • 379
  • 248.2k

How to generate sql database script according to date?

Nov 27 2013 3:31 AM
Hello,
 
I am using this code for generate script:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections.Specialized;
using System.Data.SqlClient;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;
using System.Reflection;
using Microsoft.SqlServer.Management.Sdk.Sfc;
using System.IO;
namespace GenerateScript
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void btnGenerateScript_Click(object sender, EventArgs e)
        {
 
                var server = new Server(new ServerConnection { ConnectionString = new SqlConnectionStringBuilder { DataSource = @".\SQLEXPRESS", IntegratedSecurity = true }.ToString() });
                server.ConnectionContext.Connect();
                var database = server.Databases["ankit"];
                var output = new StringBuilder();
                foreach (Table table in database.Tables)
                {
                    var scripter = new Scripter(server) { Options = { ScriptData = true } };
                    var script = scripter.EnumScript(new SqlSmoObject[] { table });
                    foreach (var line in script)
                        output.AppendLine(" "+line);
                }
 
                File.WriteAllText(@"D:\ankit.sql", output.ToString());
                MessageBox.Show("Script Created");
        }
    }
}
but i have a problem of this code, i want to generate script only current updated data or daywise script only, because my database size is too large.
 
Please help me.
 
Thanks in Advance.
 
Ankit Agarwal
Software Engineer