Restoring database using C sharp console application(SQl server 2008 R2)

Hi All,
I want to take backup and restore database from one server to another through C sharp Console Application.
My Backup is working fine but restoring is not working and throwing below error:
An unhandled exception of type 'Microsoft.SqlServer.Management.Smo.FailedOperationException' occurred in Microsoft.SqlServer.SmoExtended.dll
Database name is fine while restoring.
Please give me some ideas in resolving this.
Thanks in advance.
blrSvsTech

Hi Olaf,
I used Replace database option.
My code is something like this.
  public void Restoredb(string serverName, string useraame, string password)
            ServerConnection conn;
            if (useraame == " ") // for Windows Authentication
                SqlConnection sqlCon = new SqlConnection(@"Data Source=" + serverName + @";Integrated Security=True;");
                conn = new ServerConnection(sqlCon);
            else // for Server Authentication
                conn = new ServerConnection(serverName, useraame, password);
            Server srv = new Server(conn);
            Database database = srv.Databases["db_Miller"];
            string dbPath = Path.Combine(database.PrimaryFilePath, "db_Miller.mdf");
            string logPath = Path.Combine(database.PrimaryFilePath, "db_Miller.ldf");
            Restore restore = new Restore();
            BackupDeviceItem deviceItem = new BackupDeviceItem(path, DeviceType.File);
            restore.Devices.Add(deviceItem);
            restore.Action = RestoreActionType.Database;
            restore.ReplaceDatabase = true;
            restore.NoRecovery = true;
            restore.SqlRestore(srv);  ------------- Getting error in this line.
            database = srv.Databases["db_Miller"];
            database.SetOnline();
            srv.Refresh();
            database.Refresh(); 
Thanks in advance.
blrSvsTech
Hi blrSvsTech,
You haven’t specified the restore destination database, please try the following codes:
public void Restoredb(string serverName, string useraame, string password)
ServerConnection conn;
if (useraame == " ") // for Windows Authentication
SqlConnection sqlCon = new SqlConnection(@"Data Source=" + serverName + @";Integrated Security=True;");
conn = new ServerConnection(sqlCon);
else // for Server Authentication
conn = new ServerConnection(serverName, useraame, password);
Server srv = new Server(conn);
Database database = srv.Databases["db_Miller"];
string dbPath = Path.Combine(database.PrimaryFilePath, "db_Miller.mdf");
string logPath = Path.Combine(database.PrimaryFilePath, "db_Miller.ldf");
Restore restore = new Restore();
BackupDeviceItem deviceItem = new BackupDeviceItem(path, DeviceType.File);
restore.Devices.Add(deviceItem);
restore.Action = RestoreActionType.Database;
restore.ReplaceDatabase = true;
restore.NoRecovery = true;
restore.database = srv.Databases["db_Miller"];
restore.SqlRestore(srv); ------------- Getting error in this line.
database = srv.Databases["db_Miller"];
database.SetOnline();
srv.Refresh();
database.Refresh();
If the issue occurs again, please post all the error message here for analysis.
For more detail information, you can refer to he following link:
Restore.SqlRestore Method
http://technet.microsoft.com/en-us/library/microsoft.sqlserver.management.smo.restore.sqlrestore.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-1
Best Regards,
Allen Li
Allen Li
TechNet Community Support

Similar Messages

  • Using upgrade advisor on a database with compatibility level 80 on SQL Server 2008

    Hi All,
    My DB compatibility level is 80 which is on SqL server 2008, however I want to upgrade the database server to sql 2014. 
    Now in my case the upgrade advisor of SQL 2014 is not detecting the DB which is in 80 compatibility level and hence I cannot proceed with the upgrade (as I do not want to rollback). 
    So what exactly  can I do in this situation? any help would be appreciated
    aa

    What Olaf said Or if possible change compatibility level of database to 100 and try. I guess since you want to upgrade there wont be any issue in changing. Well if anything happens you can safely change it back to 80
    Please mark this reply as answer if it solved your issue or vote as helpful if it helped so that other forum members can benefit from it
    My Technet Wiki Article
    MVP

  • Error 18452 "Login failed. The login is from an untrusted domain and cannot be used with Windows authentication" on SQL Server 2008 R2 Enterprise Edition 64-bit SP2 clustered instance

    Hi there,
    I have a Windows 2008 R2 Enterprise x64 SP2 cluster which has 2 SQL Server 2008 R2 Enterprise Edition x64 SP2
    instances.
    A domain account "Domain\Login" is administrator on both physcial nodes and "sysadmin" on both SQL Server instances.
    Currently both instances are running on same node.
    While logging on to SQL Server instance 2 thru "Domain\Login" using "IP2,port2", I get error 18452 "Login failed. The login is from an untrusted domain and cannot be used with Windows authentication". This happened in the past
    as well but issue resolved post insatllation of SQL Server 2008R2 SP2. This has re-occurred now. But it connects using 'SQLVirtual2\Instance2' without issue.
    Same login with same rights is able to access Instance 1 on both 'SQLVirtual1\Instance1' and "IP1,port1" without any issue.
    Please help resolve the issue.
    Thanks,
    AY

    Hello,
    I Confirm that I encountred the same problem when the first domain controller was dow !!
    During a restarting of the first domain controller, i tried to failover my SQL Server instance to a second node, after that I will be able to authenticate SQL Server Login but Windows Login returns Error 18452 !
    When the firts DC restart finishied restarting every thing was Ok !
    The Question here : Why the cluster instance does'nt used the second DC ???
    Best Regards     
    J.K

  • Difference between database features - SQL Server 2008 R2 navtive vs SQL Server 2012 with SQL Server 2008 compatibility set?

    I am investigating the impact of upgrading from SQL Server 2008 R2 to SQL Server 2012. To reduce the impact of the upgrade, I am planning to restore / create our application database onto SQL Server 2012 with compatibility level 100 (SQL Server 2008 and
    SQL Server 2008 R2).
    Are there any differences in feature support for database running in native mode on SQL Server 2008 R2 vs a database installed on SQL Server 2012 with compatibility level 100 set?

    Are there any differences in feature support for database running in native mode on SQL Server 2008 R2 vs a database installed on SQL Server 2012 with compatibility level 100 set?
    Yes there can be difference and impact there are few features deprecated in SQL Server 2012 you must be aware about that. Please see
    Deprecated Database Features in SQL Server 2012
    Deprecated SQL Server features in SQL Server 2012
    After you migrate database to 2012 please don't move ahead with production unless you have tested your application to new created database
    Please mark this reply as answer if it solved your issue or vote as helpful if it helped so that other forum members can benefit from it
    My Technet Wiki Article
    MVP

  • Isse with using Grouping Sets in SQL Server 2008 R2

    Hi,
    I created a query in SQL Server 2012 using grouping sets that works fine.  When I try to use the same query in SQL Server 2008 I get an error ("Inccorrect syntax near SETS").
    I researched using grouping sets in 2008 and didn't see any issue with my query.  What is different in grouping sets 2008 vs 2012?
    SELECT tl.ClientRegionCd as [Client Region Code] , tl.ClientRegionDesc as [Region Name], count(tl.CompleteICN) as [Trauma Letters Sent]
    from TORT_Trauma_Letters tl
    Where CONVERT(VARCHAR(26), tl.SecondNoticeSent, 23) between '2014-06-12' and '2014-06-12'
    GROUP BY GROUPING SETS((tl.ClientRegionCd, tl.ClientRegionDesc), ())
    Stacie

    Check this blog post as how to deal with date ranges
    http://sqlblog.com/blogs/aaron_bertrand/archive/2009/10/16/bad-habits-to-kick-mishandling-date-range-queries.aspx
    For your result you can simply achieve your result with UNION ALL, e.g.
    select Client, Region, Letters
    from myTable 
    UNION ALL
    select NULL, NULL, SUM(letters) as Letters
    For every expert, there is an equal and opposite expert. - Becker's Law
    My blog
    My TechNet articles

  • Why i can not use dbco to connect sql server 2008?

    Dear Experts,
        i use this mehtod to link sql server 2005 :MSSQL_SERVER=tcp:mysqlserver IP  MSSQL_DBNAME=SubConPayment,
    it is ok,when i use this method to link sql server 2008 with dbco,it is not ok,can you tell me why i can not link to sql server2008 in sap with dbco?
    looking forward to your reply.
    Best regards,
    Merry

    Hi Merry,
    Seems that you have to add the "provider" attribute to the connectionstring. Please have a look at the following link: [http://www.connectionstrings.com/sql-server-2008#p2]
    Regards,
    Ozcan.

  • Oracle 10g DB Export for SQL Server 2008

    I'm having a hard time exporting my database Primavera P6 Project Management for SQL Server 2008, in fact I need to do this so I can export to a future program in Visual Studio 2010. But contiue with the Oracle Database Enabled
    Thank you.
    Edited by: 898987 on 25/11/2011 01:59

    Hi,
    Thanks for the translation.
    You say you are having a hard time exporting my database Primavera P6 Project Management for SQL Server 2008. Could you give details of exactly what you are using for the export and what problems you are having ?
    Is this an export from an Oracle database or a SQL*Server 2008 database ?
    Regards,
    Mike

  • Can't connect to sql server 2008

    Hi.
    I have 2 databases running, oracle 10g express and sql server 2008 express.
    I can connect with sql developer to the oracle database, but cant establish a connection to the sql server database.
    Using the Microsoft SQL Server Management Studio i can access the database using the same parameters as in sql developer.
    I already downloaded the third party driver to access sql server databases.

    I found this solution out there on the Internet...
    http://djiang.wordpress.com/2007/02/27/using-oracle-sqldeveloper-to-access-sqlserver/
    This might help someone else, it worked for me!
    Use this connection settings:
    Connection Name: MyDataBaseName
    Username: user
    Password: password
    Hostname: localhost
    Port:1433/databaseName;instance=SQLEXPRESS

  • Incompatible SQL backup on SQL Server 2008 R2

    I installed SQL Server 2008 R2 from MSDN download for testing on a development server. I copied a backup from a live SQL Server 2008 R2 server and when I tried to restore the database on the MSDN downloaded copy of SQL the following error was generated...
    "The database was backed up on server version 10.50.4033. That version is incompatible with this server, which is running version 10.00.2531....."
    Any suggestions on a solution?

    NO possibilities of backing up from higher to lower version.
    only lower to higher is possible.
    you can do this
    upgrade this instance to SQL Server 2008 R2 or
    restore the backup you have on a SQL Server 2008 R2 instance, export all the data and import it on a SQL Server 2008 database.
    Mark as answer if it is useful

  • Has mssql2008r2 got "Cumulative Update Package 6 for SQL Server 2008 Service Pack 1"

    hi,
    I have
    Microsoft SQL Server 2008 R2 (RTM) - 10.50.1600.1 (Intel X86)   Apr  2 2010 15:53:02   Copyright (c) Microsoft Corporation  Express Edition with Advanced Services on Windows NT 5.1 <X86> (Build 2600: Service Pack 3) 
    , i wanted to know has it got
    "Cumulative Update Package 6 for SQL Server 2008 Service Pack 1"
    so that i do not have to install or i have to install
    yours sincerley

    Thank u, please tel me can i instal  sp3.
    I have noticed the link of sp2 (http://www.microsoft.com/en-in/download/details.aspx?id=30437)
     has listed
    (SQL Server 2008 R2 Express Edition)
    SQL Server 2008 R2 Service Pack 2 (SP2) is now available for download. SQL Server 2008 R2 service packs are cumulative and can be used to upgrade all releases of SQL Server 2008 R2 to Service Pack 2. SQL Server 2008 R2 Service Pack 2 contains Cumulative Update
    1 to 5 from SQL Server 2008 R2 SP1. The package can be used to upgrade the following SQL Server 2008 R2 editions: 
    SQL Server 2008 R2 Parallel Computing Edition
    SQL Server 2008 R2 Datacenter Edition
    SQL Server 2008 R2 Enterprise Edition and Developer Edition
    SQL Server 2008 R2 Standard Edition
    SQL Server 2008 R2 Web Edition
    SQL Server 2008 R2 Workgroup Edition
    SQL Server 2008 R2 Express Edition
    where as link of sp3(http://www.microsoft.com/en-us/download/details.aspx?id=44271)
    has not listed express edition but written in general.
    SQL Server 2008 R2 Service Pack 3 (SP3) is now available for download. SQL Server 2008 R2 service packs are cumulative and can be used to upgrade all releases of SQL Server 2008 R2 to Service Pack 3. SQL Server 2008 R2 Service Pack 3 contains
    Cumulative Update 1 to 13 from SQL Server 2008 R2 SP2. The package can be used to upgrade the following SQL Server 2008 R2 editions: 
    SQL Server 2008 R2 Parallel Computing Edition
    SQL Server 2008 R2 Datacenter Edition
    SQL Server 2008 R2 Enterprise Edition and Developer Edition
    SQL Server 2008 R2 Standard Edition
    SQL Server 2008 R2 Web Edition
    SQL Server 2008 R2 Workgroup Edition
    yours sincerley

  • Fixed Assets instalation on SAP Business One 2007 A PL 47 SQL Server 2008

    Dear Experts,
    When i try to start the Fixed Assets Addon following Error occured
    System Message
    Set-up function of add-onfailed(C:Program Files\SAP\SAP Business One\Add-ons\SAP\Fixed Assets\BA_FixedAssets.exe)
    I am using SBO 2007A PL 47 & SQL Server 2008
    Kindly suggest
    Tahir

    hi,
    Answered in this link
    Fixed Asset Add-on Failed to Install
    Jeyakanthan

  • Set SPN for a local SQL Server 2008 R2 Express database used for local "Offline Mode" application

    Hi - I have tried searching the web for tips on this situation. Basically, I am trying to recreate a previous employee's client Image. This image is used for one of our companies client application that features and "Offline Mode" where it connects
    (Using Kerberos) to the local DB instead of the central DB server.
    The custom image connects without issue. I cannot seem to recreate his configuration. I am sure of these things:
    - Both images are using the same SQL Server 2008 R2 Express version.
    - Both images have the same user accounts setup.
    - Both images have "Remote Connections" setup with TCP/IP enabled in the Configuration Manager.
    - Both images are joined to the same Domain server currently.
    - Both images has the SQLServer service set to use "NETWORK SERVICE" account.
    This is the error message I get when SQL starts up for the non-working image:
    The SQL Network Interface library could not register the Service Principal Name (SPN) for the SQL Server service. Error: 0x54b, state: 3. Failure to register an SPN may
    cause integrated authentication to fall back to NTLM instead of Kerberos. This is an informational message. Further action is only required if Kerberos authentication is required by authentication policies.
    Thank you for any assistance given.

    Hi Gribbled,
    Could you please change the SQL Server Service to run under 'LocalSystem' account and restart SQL Server? Then check if the error still occurs. When the SQL Server service account is configured to use the LocalSystem account, the server will automatically
    publish the SPN.
    Alternatively, to work around this issue, you can manually create the SPN for the SQL Server Service account  using the SETSPN tool. For more details, please review this
    blog.
    Thanks,
    Lydia Zhang

  • Can I use Same ADF application with SQL server 2008 as well as Oracle 11g?

    Hi ,
    I have created a application in ADF(Using J-Developer). I used SQL server 2008 as Database Provider. I have same Database in Oracle 11g. I want to connect my appliction to Oracle 11g database. I changed connection and connected to Oracle. But, when I tried to save or delete any data from front end, it gave error. Any solution on this?
    Thanks

    Hi,
    I have created Entity object and View object for every page in my application. That objects created from sql server database. Application is working fine with sql server. But when connected with Oracle 11g. It is giving following errors:
    On clicking search control it gives: ORA 00923 From keyword not found where expected.
    On searching with perticular field it gives: ORA-01722: Invalid Number.
    On Clicking save buttonit gives: ORA-00933: SQL command not properly ended.
    These are error messages from IntegratedWebLogicServer-Log:
    java.sql.SQLSyntaxErrorException: ORA-00923: FROM keyword not found where expected
    <QueryCollection> <buildResultSet> [3929] java.sql.SQLSyntaxErrorException: ORA-00923: FROM keyword not found where expected
    Caused by: java.sql.SQLSyntaxErrorException: ORA-00923: FROM keyword not found where expected
    <DCBindingContainer> <cacheException> [3947] * * * BindingContainer caching EXCEPTION:oracle.jbo.SQLStmtException
    <DCBindingContainer> <cacheException> [3948] java.sql.SQLSyntaxErrorException: ORA-00923: FROM keyword not found where expected
    <DCBindingContainer> <cacheException> [3949] * * * BindingContainer caching EXCEPTION:oracle.jbo.SQLStmtException
    <DCBindingContainer> <cacheException> [3950] java.sql.SQLSyntaxErrorException: ORA-00923: FROM keyword not found where expected
    <DCBindingContainer> <cacheException> [3951] * * * BindingContainer caching EXCEPTION:oracle.jbo.SQLStmtException
    <DCBindingContainer> <cacheException> [3952] java.sql.SQLSyntaxErrorException: ORA-00923: FROM keyword not found where expected
    <DCBindingContainer> <cacheException> [3953] * * * BindingContainer caching EXCEPTION:oracle.jbo.SQLStmtException
    <DCBindingContainer> <cacheException> [3954] java.sql.SQLSyntaxErrorException: ORA-00923: FROM keyword not found where expected
    java.lang.NullPointerException

  • Problem with restore database in sql server 2008

    hi,,,when i want to restore database from a .bak file i see this error
    TITLE: Microsoft SQL Server Management Studio
    Restore failed for Server 'ALI-PC'.  (Microsoft.SqlServer.SmoExtended)
    For help, click: http://go.microsoft.com/fwlink?ProdName=Microsoft+SQL+Server&ProdVer=10.50.1447.4+((KJ_RTM).100213-0103+)&EvtSrc=Microsoft.SqlServer.Management.Smo.ExceptionTemplates.FailedOperationExceptionText&EvtID=Restore+Server&LinkId=20476
    ADDITIONAL INFORMATION:
    System.Data.SqlClient.SqlError: The database was backed up on a server running version 10.50.4000. That version is incompatible with this server, which is running version 10.00.1600. Either restore the database on a server that supports the backup, or use a
    backup that is compatible with this server. (Microsoft.SqlServer.Smo)
    For help, click: http://go.microsoft.com/fwlink?ProdName=Microsoft+SQL+Server&ProdVer=10.50.1447.4+((KJ_RTM).100213-0103+)&LinkId=20476
    BUTTONS:
    OK
    i checked overwrite the existing database(WUTH REPLACE) in option,but i cant restore again,,,please help me

    Hi,
    You need to install SQL Server 2008 R2 Service Pack 2
    Download
    http://www.microsoft.com/en-us/download/details.aspx?id=11093, you are currently running the RTM and the backup was from Version 10.50.4000 and your attempting to restore to version 10.00.1600.
    To find your current SQL Version open a New Query and paste the following then execute
    SELECT @@VERSION AS 'SQL Server Version
    Reference:
    http://sqlserverbuilds.blogspot.com/
    http://support.microsoft.com/kb/321185
    http://technet.microsoft.com/en-us/library/ms177512.aspx
    Cheers,
    -Ivan

  • How to restore SQL Server 2008 database to SQL Server Express 2012

    How to restore SQL Server 2008 database to SQL Server Express 2012.
    I BACKED UP a SQL Server 2008 database from work, which runs the enterprise version of 2008, and I tried to RESTORE it to my new 64-Bit home PC which is running the new SQL Server Express 2012 64-Bit (Advanced Edition).
    But if fails. I get the following message text.
    Can anyone help me please. I thought restoring a 2008 database to the later version 2012 would be possible.
    Here is the error message. Thanks.
    TITLE: Microsoft SQL Server Management Studio
    Restore of database failed. (Microsoft.SqlServer.Management.RelationalEngineTasks)
    ADDITIONAL INFORMATION:
    System.Data.SqlClient.SqlError: The database was backed up on a server running version 8.00.2039. That version is incompatible with this server, which is running version 11.00.3128. Either restore the database on a server that supports the backup, or use a
    backup that is compatible with this server. (Microsoft.SqlServer.SmoExtended)

    ADDITIONAL INFORMATION:
    System.Data.SqlClient.SqlError: The database was backed up on a server running version 8.00.2039. That version is incompatible with this server, which is running version 11.00.3128. Either restore the database on a server that supports the
    backup, or use a backup that is compatible with this server. (Microsoft.SqlServer.SmoExtended)
    Are you sure your database is SQL server 2008 from error message it seems your SQL server version is 2000.I guess you have multiple versions of SQL server.
    AFAIK you cannot restore backup of SQL server 2000 to 2012 directly .Restore it first to SQL 2008/2008R2 or 2005 then take backup again of new database created and then you would be able to restore it on 2012.Its kind of intermediate way
    Also make sure your DB size is less than 10 G as max DB size supported by Express edition is 10 G and if your DB size is more than 10 G restore will fail.
    You are also trying to restore enterprise DB backup on express edition so you will loose enterprise features on express edition
    Please mark this reply as the answer or vote as helpful, as appropriate, to make it useful for other readers

Maybe you are looking for