Saturday 31 January 2015

How to create duplicate table in different schema name in Asp.net (create new table in different schema)

Process I am writting
1. First  open a wordpad File.
2.Next write a table which in your database on that word file.
       Example
     CREATE TABLE [dbo].[linku] (
    [Id]               INT          IDENTITY (1, 1) NOT NULL,
    [Pid]              INT          NULL,
    [Achievement_Name] VARCHAR (50) NULL,
    [Year]             VARCHAR (4)  NULL,
    [IsDeleted]        BIT          NULL,
    PRIMARY KEY CLUSTERED ([Id] ASC),
    FOREIGN KEY ([Pid]) REFERENCES [dbo].[brahmananda_profile] ([Id])
    )


CREATE TABLE [dbo].[linku_education] (
    [Id]                 INT             IDENTITY (1, 1) NOT NULL,
    [Pid]                INT             NULL,
    [Education]          VARCHAR (60)    NULL,
    [BoardUniversity]    VARCHAR (70)    NULL,
    [Institution]        VARCHAR (60)    NULL,
    [Year_Of_Passing]    VARCHAR (4)     NULL,
    [Percentage_Secured] DECIMAL (18, 2) NULL,
    [IsDeleted]          BIT             NULL,
    PRIMARY KEY CLUSTERED ([Id] ASC),
    FOREIGN KEY ([Pid]) REFERENCES [dbo].[brahmananda_profile] ([Id])
)
  comment:  you can add so much table you want to add

3. save this Wordfile on the name of table.sql.
4. Next copy that word file and paste on .Net Solution on App_Data folder.
5.Next add a web page on the .net solution.
6. and add a design  .

   example in design Source page.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Schema.aspx.cs" Inherits="TestSwash.Schema" %>

<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <telerik:RadScriptManager ID="RadScriptManager1" runat="server"></telerik:RadScriptManager>
    <div>
        <telerik:RadTextBox ID="RadTextBox1" runat="server"></telerik:RadTextBox><telerik:RadButton ID="RadButton1" runat="server" Text="Schema" OnClick="RadButton1_Click"></telerik:RadButton>
    </div>
    </form>
</body>
</html>

7. Next go to page behind C# file code.

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Configuration;
using System.Data.SqlClient;
using System.IO;
using System.Text;

namespace TestSatya
{
    public partial class Schema : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void RadButton1_Click(object sender, EventArgs e)
        {
            string conn_str = WebConfigurationManager.ConnectionStrings["test"].ConnectionString;
            //Response.Write(conn);
            string sql_path = Path.Combine(Server.MapPath("~/App_Data"), "table.sql");
            string script = File.ReadAllText(sql_path);
          //  Response.Write(script);
            SqlConnection con = new SqlConnection(conn_str);
            script=script.Replace("[dbo]","["+RadTextBox1.Text+"]");
           // string sql_exe = "CREATE SCHEMA  " + RadTextBox1.Text + " " + script;

            //SqlCommand cmd=new SqlCommand()
            //Response.Write(sql_exe);
            con.Open();
          //  SqlCommand cmd = new SqlCommand("SELECT name FROM sys.tables WHERE  OBJECT_SCHEMA_NAME(object_id(name)) ='DBO'", con);
          //  //SqlCommand cmd = new SqlCommand(sql_exe, con);
          //  SqlDataReader rd = cmd.ExecuteReader();
          // // StringBuilder bld = new StringBuilder("");

          //  while(rd.Read())
          //  {
          //      bld.Append(rd["name"].ToString()+",");
          //      //rd.NextResult();
          //      //---------------------
          //      if (rd["name"].ToString().Equals("brahmananda_achievement") || rd["name"].ToString().Equals("brahmananda_education") || rd["name"].ToString().Equals("brahmananda_profile"))
          //      {
          //          script = script.Replace(rd["name"].ToString(), RadTextBox1.Text + "_" + rd["name"].ToString());
          //      }
          //      //----------------------
          //  }
          ////  cmd.ExecuteNonQuery();
          ////  Response.Write(bld.ToString());
          //  rd.Close();
            string sql_exe = "CREATE SCHEMA  " + RadTextBox1.Text + " " + script;
          //  Response.Write(sql_exe);
            SqlCommand cmd = new SqlCommand(sql_exe, con);
            //cmd.CommandText = sql_exe;
            cmd.ExecuteNonQuery();
            con.Close();
            Response.Write("Schema created");
        }
    }
}

=======================================================

After that you see.
  
Let your database name is Sairam and table name is dbo.linku
i want to chane the schema means where dbo on that palace i want to change user.linku
There fore on runtime i input on the text box user  and after save .
Now you see in your database your schema will be change.


======================================


 Thanks
  Satyabrata behera




  

No comments:

Post a Comment