You must Sign In to post a response.
  • Category: SQL Server

    How to copy tables from one database to another?

    I want to copy database tables from one database which is sql database to service based database of windows forms application. how to do that ?
  • #761261
    You can use the "Import/Export" option in the sqlserver, where you can select the table which you want to copy.
    By Nathan
    Direction is important than speed

  • #761263
    select Db. right click
    select tasks select Generate scripts
    Once more select db, next, next and finish. you will get script. you can run by changing the new db name in the first part of the script

    Do Good... Enjoy your life.....

  • #761266
    You can try to Backup and Restore for this

    Backup CODE


    BACKUP DATABASE TEST TO DISK='d:\TestDB.bak'


    Restore CODE

    RESTORE DATABASE Test FROM DISK='d:\TestDB.bak'

    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #761267
    Hello Priyanka,

    Refer the below code :

    string fromdbname = "existing database";
    TextBoxDBName.Text = "new database";

    con.Open();
    mycmd = new SqlCommand("SELECT SCHEMA_NAME as DB FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '" + TextBoxDBName.Text + "';",con);
    dr = mycmd.ExecuteReader();
    if (!dr.HasRows)
    {
    //creating database with existing database's tables n their records
    cmd.Connection = con1;
    con1.Open();
    cmd.CommandText = "CREATE DATABASE IF NOT EXISTS `" + TextBoxDBName.Text + "`;";
    cmd.ExecuteNonQuery();
    cmd.CommandText = "CREATE TABLE `" + TextBoxDBName.Text + "`.`BankMaster` SELECT * FROM `" + fromdbname + "`.`BankMaster`;";
    cmd.ExecuteNonQuery();
    cmd.CommandText = "CREATE TABLE `" + TextBoxDBName.Text + "`.`CashMemo` SELECT * FROM `" + fromdbname + "`.`CashMemo` WHERE Memo_Date >= '" + DBStartDate.Text + "';";
    cmd.ExecuteNonQuery();
    cmd.CommandText = "CREATE TABLE `" + TextBoxDBName.Text + "`.`MemoDetail` SELECT * FROM `" + fromdbname + "`.`MemoDetail` WHERE Memo_Date >= '" + DBStartDate.Text + "';";
    cmd.ExecuteNonQuery();
    con1.Close();
    }
    con.Close();

    I have written some query which i have used to achieve this task.

    Hope this is the actual you require.

    Regards,
    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"

  • #761269
    I attach mdf database file in sql server managment studio import data into that and detach from there and when i opened this project in vs and try to access database it gives version problem .

    "The database 'C:\DAVA_SINGLECLIENT\DAVA_SINGLECLIENT\DATABASE1.MDF' cannot be opened because it is version 661. This server supports version 655 and earlier. A downgrade path is not supported.
    Could not open new database 'C:\DAVA_SINGLECLIENT\DAVA_SINGLECLIENT\DATABASE1.MDF'. CREATE DATABASE is aborted.
    An attempt to attach an auto-named database for file C:\Dava_SingleClient\Dava_SingleClient\Database1.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share."

  • #761275
    Hello Priyanka,

    You should take backup of data before doing R&D on database.

    You can not downgrade the version of database. This is the lack of flexibility from Microsoft SQL Server.

    You have to recreate the database again.
    Create it's scripts (queries) and save them into .sql file then you can use that .sql file anywhere and anytime.

    Hope this will help you.

    Regards,
    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"

  • #761277
    @Nirav sir , should i write actual database names in string fromdbname and txtboxdbname ,
    and what is memo date is this a column or what?

  • #761279
    Hi

    I have mention the Code runtime Create table in sql server from c# try this code

    Client Side

    <asp:TextBox ID="TextBoxDBName" runat="server"></asp:TextBox>
    <asp:Button ID="Btsubmit" runat="server" Text="Submit" OnClick="Btsubmit_Click" />


    Server Side

    protected void Btsubmit_Click(object sender, EventArgs e)
    {
    string SQRY = "USe Test;Create Table " + TextBoxDBName.Text + "(Id int);";
    SqlConnection con = new SqlConnection(strCon);
    con.Open();
    SqlCommand sqlcm = new SqlCommand(SQRY, con);
    sqlcm.ExecuteNonQuery();

    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #761283
    Hai Priyanka,
    It seems that your old database is in lower version and you are trying to import in higher version, which will not be supported.
    So try to generate the script for the whole database and then get the script and run in to new database.
    By this way, you will be able to import all data.
    Hope it will be helpful to you.

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com

  • #761284
    try generate script ... the process i mentioned in above....
    Do Good... Enjoy your life.....

  • #761295
    I think just simple as export database table to excel sheet and then import them to new one. Is this easy one that generating script??

  • #761301
    Hello Priyanka,

    fromfbname means existing database (old database)
    txtdbname means new database that you will create.

    MemoDate is the column name of table.

    I think the method i told you it is very easy as i have used it in one of my project.

    Regards,
    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"

  • #761302
    Hi

    If you need datas you can try

    export database table to excel sheet and then import them to new one.

    No need Data only Table struture means you can try Generate table script .

    If you go Both option first your mdf file support correct version mdf having in your sql server then only you can export excel and generate script.

    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.


Sign In to post your comments