Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
290 views
in Technique[技术] by (71.8m points)

c# - LocalDB: change SQL Server default location

I wonder if it is possible to change default location of (LocalDB). When you create it with SqlLocalDB.exe default location is

C:UsersuserIdAppDataLocalMicrosoftMicrosoft SQL Server Local DBInstancesMyDB

And I believe this path is used in (LocalDB) in connection strings (auto generated by creator of *.dbml files):

<connectionStrings>
    <add name="MyApp.Properties.Settings.MyConnectionString"
         connectionString="Data Source=**(LocalDB)**MyDB;Initial Catalog=sthDB;Integrated Security=True"
         providerName="System.Data.SqlClient" />
</connectionStrings>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I've also been trying to customise the instances location, and have found a solution. As alluded to in previous posts, it appears that it defaults to %LOCALAPPDATA%MicrosoftMicrosoft SQL Server Local DBInstances. After some experimentation, it seems that the SQLLocabDB command line utility uses the %USERPROFILE% environment variable (rather than %LOCALAPPDATA%) to find this location.

The following worked for me (using SQLLocalDB from a command prompt):

C:Usersdan.smith>echo %USERPROFILE%
C:Usersdan.smith

C:Usersdan.smith>set USERPROFILE=c:emp

C:Usersdan.smith>echo %USERPROFILE%
c:emp

C:Usersdan.smith>mkdir c:empAppDataLocal

C:Usersdan.smith>sqllocaldb create test
LocalDB instance "test" created with version 13.0.1100.286.

C:Usersdan.smith>cd C:empAppDataLocalMicrosoftMicrosoft SQL Server Local DBInstancesest

C:empAppDataLocalMicrosoftMicrosoft SQL Server Local DBInstancesest>dir /w
 Volume in drive C has no label.
 Volume Serial Number is 4A71-7A6F

 Directory of C:empAppDataLocalMicrosoftMicrosoft SQL Server Local DBInstancesest

[.]                                      [..]
error.log                                error1.log
log.trc                                  master.mdf
mastlog.ldf                              model.mdf
modellog.ldf                             msdbdata.mdf
msdblog.ldf                              system_health_0_131061520581180000.xel
tempdb.mdf                               templog.ldf
              12 File(s)     46,701,550 bytes
               2 Dir(s)  117,107,499,008 bytes free

As shown, this created my LocalDB instance under c:emp, albeit inheriting the original folder hierarchy from "AppData" onward (which seems unchangeable). Note that it was also necessary to create the "AppDataLocal" part of the folder hierarchy manually, otherwise it fails.

The next issue is actually connecting to this database from a C# application. To do this, the %USERPROFILE% environment variable must be set to the same location as above, i.e.:

Environment.SetEnvironmentVariable("USERPROFILE", "c:\temp");

This should be done prior to establishing a DB connection. Probably best to do this somewhere in the entry point of the application.

All in all this is a bit of a hack, but it at least gives one the option to store things somewhere else other than in "c:users...".

UPDATE

It's worth noting that the idea here was to only change the %USERPROFILE% environment variable for the currently running process, rather than machine-wide. This is the behaviour when using set in the command prompt. The Environment.SetEnvironmentVariable method overload mentioned above also defaults to this behaviour, though it's probably better to be more explicit with something like:

Environment.SetEnvironmentVariable("USERPROFILE", "c:\temp", EnvironmentVariableTarget.Process);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...