简体   繁体   中英

How to use backup .sql file to add databse to sql server using C# console application?

I was working on a project where i made a database on sql server 2008 using visual studio 2010 and i took backup of that databse by right clicking on it in sever explorer and selecting "Publish to Provider" that creates a .sql file in my computer that i taken. Previously i formatted my pc and sql server databases are lost, so now i want to use that .sql file to recreate the database with all data, but i don't know how it can be possible.

Something i tried but it created all tables in master database but i want the database of the name ie stored in .sql file or specified by me.

Please help.

First Create a Database using this,

Create Database YOURDBNAME

Here, YOURDBNAME = DataBase Name

Then, Open your .sql file write this in First line of the .sql page,

Use YOURDBNAME

Now press F5 , all the tables and data will be created and inserted.

You can use the SQL Server Management Objects libraries to manage all aspects of SQL Server.

SQL Server Management Objects (SMO) is a collection of objects that are designed for programming all aspects of managing Microsoft SQL Server. SQL Server Replication Management Objects (RMO) is a collection of objects that encapsulates SQL Server replication management.


From your description, however, this can be overkill. You get simply use SQL Server Management Studio (link goes to the free Express edition download), connect to the server, open a new Query Window with your SQL file and run it (F5).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM