SQL Server: Importing database from .mdf?

后端 未结 5 1136
梦毁少年i
梦毁少年i 2021-02-01 13:30

I have an .mdf file on my local box.

I have SQL Server 2008 Express and SQL Management Studio 2008 Express installed on my local box.

How in the world do I impor

相关标签:
5条回答
  • 2021-02-01 14:11

    Apart from steps mentioned in posted answers by @daniele3004 above, I had to open SSMS as Administrator otherwise it was showing Primary file is read only error.

    Go to Start Menu , navigate to SSMS link , right click on the SSMS link , select Run As Administrator. Then perform the above steps.

    0 讨论(0)
  • 2021-02-01 14:13

    Open SQL Management Studio Express and log in to the server to which you want to attach the database. In the 'Object Explorer' window, right-click on the 'Databases' folder and select 'Attach...' The 'Attach Databases' window will open; inside that window click 'Add...' and then navigate to your .MDF file and click 'OK'. Click 'OK' once more to finish attaching the database and you are done. The database should be available for use. best regards :)

    0 讨论(0)
  • 2021-02-01 14:34

    To perform this operation see the next images:

    and next step is add *.mdf file,

    very important, the .mdf file must be located in C:......\MSSQL12.SQLEXPRESS\MSSQL\DATA

    Now remove the log file

    0 讨论(0)
  • 2021-02-01 14:35

    If you do not have an LDF file then:

    1) put the MDF in the C:\Program Files\Microsoft SQL Server\MSSQL13.SQLEXPRESS\MSSQL\DATA\

    2) In ssms, go to Databases -> Attach and add the MDF file. It will not let you add it this way but it will tell you the database name contained within.

    3) Make sure the user you are running ssms.exe as has acccess to this MDF file.

    4) Now that you know the DbName, run

    EXEC sp_attach_single_file_db @dbname = 'DbName', 
    @physname = N'C:\Program Files\Microsoft SQL Server\MSSQL13.SQLEXPRESS\MSSQL\DATA\yourfile.mdf';
    

    Reference: https://dba.stackexchange.com/questions/12089/attaching-mdf-without-ldf

    0 讨论(0)
  • 2021-02-01 14:36

    See: How to: Attach a Database File to SQL Server Express

    Login to the database via sqlcmd:

    sqlcmd -S Server\Instance
    

    And then issue the commands:

    USE [master]
    GO
    CREATE DATABASE [database_name] ON 
    ( FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\Data\<database name>.mdf' ),
    ( FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\Data\<database name>.ldf' )
     FOR ATTACH ;
    GO
    
    0 讨论(0)
提交回复
热议问题