Click here to Skip to main content
15,906,097 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have to create a new database from DataContext (dbml) and attach it to sqlserver
I have created the new database but the attach method doesn't work

What I have tried:

C#
 MyDataContext db = new MyDataContext (connstr);
            db.CreateDatabase();
            System.Diagnostics.Process process = new System.Diagnostics.Process();
            System.Diagnostics.ProcessStartInfo startInfo = new                                                                                        System.Diagnostics.ProcessStartInfo();
            startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
            startInfo.FileName = "cmd.exe";
            startInfo.Arguments = "sqlcmd -S localhost\\SQLexpress"+ Environment.NewLine +
"USE [master]"+ Environment.NewLine +
"GO"+ Environment.NewLine +
"CREATE DATABASE MyNewDb ON "+ Environment.NewLine +
"( FILENAME = N'C:\\Program Files\\Microsoft SQL Server\\MSSQL10_50.SQLEXPRESS\\MSSQL\\DATA\\MyNewDb.mdf' ),"+ Environment.NewLine +
"( FILENAME = N'C:\\Program Files\\Microsoft SQL Server\\MSSQL10_50.SQLEXPRESS\\MSSQL\\DATA\\MyNewDb.ldf' )"+ Environment.NewLine +
" FOR ATTACH ;"+ Environment.NewLine +
"GO";
            process.StartInfo = startInfo;
            process.Start();
            process.WaitForExit();
Posted
Updated 19-Jul-16 19:53pm
v2

1 solution

( FILENAME = N'C:\\Program Files\\Microsoft SQL Server\\MSSQL10_50.SQLEXPRESS\\MSSQL\\DATA\\MyNewDb.ldf' )

Check Extensions of both items one is mdf and other is ldf
 
Share this answer
 
Comments
M. Daban 20-Jul-16 3:38am    
checked, right extentions
M. Daban 20-Jul-16 3:40am    
and i can attach it manually using management studio

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900