sql - Releasing an .MDF File in VB.NET -


i have application checks see if it's recent version. if not, updates using file.copy replace db attached application fresh 1 (that may or may not have had changes made it). in attempt keep data being deleted, created backup system writes data xml file before database deleted , restores data once database has been copied.

i having problem file.copy method, however, in error pops telling me .mdf being used process.

i told stopping sql server work, hasn't. i've been told can use smo, have not been able make work. seeming close complete, smo seems won't necessary.

my code this:

'this backup. make sure close sql connection when process complete.          dim db string = "c:\ace db\localacetest.mdf"         dim dblog string = "c:\ace db\localacetest_log.ldf"          if file.exists(db) = true               'backup process               '...         end if          '"data/localacetest.mdf" referenced below file located inside of application used overwrite other mdf; not .mdf i'm looking replace.         directory.createdirectory("c:\random directory\")        file.copy("data/localacetest.mdf", db, true) 'this line error        file.copy("data/localacetest_log.ldf", dblog, true)        success = false        ... 

edits: have narrowed issue down method backs data. i'm using following connection string:

private const _sqldb string = "data source=(localdb)\v11.0;initial catalog=localacetest;integrated security=true; _ attachdbfilename=c:\ace db\localacetest.mdf" 

i open sql, run command, , close it:

using connection = new sqlconnection(_sqldb)             connection.open()             ...             connection.close() 

why not release mdf process? (when don't run it, have no problems)

you better off sending command sql server telling make backup you. article has great (command line) script can copy/paste: sql server command line backup statement

put batch , launch this.

system.diagnostics.process.start("c:\ace db\makeadbbackup.bat") 

if prefer make program wait till backup finishes, read launching processes: http://support.microsoft.com/kb/305368


if insistent on making copy of .mdf (which not idea), need ask server stop sql service before make copy. run batch says

net stop mssqlserver 

assuming sql server running under name "mssqlserver". check names of running services, open command prompt , type-in "net start". give list of services running. 1 of them "process name" of sql server's running service.

better still, here article (for vb.net) shows source code starting/stopping sql server. http://msdn.microsoft.com/en-us/library/ms162139(v=sql.90).aspx

i recommend try first approach suggested.


Comments

Popular posts from this blog

java - JavaFX 2 slider labelFormatter not being used -

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -