如何从代码中设置Sql Server中的默认数据库?

时间:2022-09-03 23:18:19

I can't seem to figure out how to set the default database in Sql Server from code. This can be either .Net code or T-Sql (T-Sql would be nice since it would be easy to use in any language). I searched Google and could only find how to do it in Sql Server Management Studio.

我似乎无法弄清楚如何从代码中设置Sql Server中的默认数据库。这可以是.Net代码或T-Sql(T-Sql会很好,因为它很容易在任何语言中使用)。我搜索了Google,只能在Sql Server Management Studio中找到它。

4 个解决方案

#1


14  

from: http://doc.ddart.net/mssql/sql70/sp_da-di_6.htm

来自:http://doc.ddart.net/mssql/sql70/sp_da-di_6.htm

sp_defaultdb [@loginame =] 'login' , [@defdb =] 'database'

#2


30  

ALTER LOGIN should be used for SQL Server 2005 or later:

ALTER LOGIN应该用于SQL Server 2005或更高版本:

http://technet.microsoft.com/en-us/library/ms189828.aspx

http://technet.microsoft.com/en-us/library/ms189828.aspx

ALTER LOGIN <login_name> WITH DEFAULT_DATABASE = <default_database>

sp_defaultdb eventually will be removed from SQL Server:

最终将从SQL Server中删除sp_defaultdb:

http://technet.microsoft.com/en-us/library/ms181738.aspx

http://technet.microsoft.com/en-us/library/ms181738.aspx

#3


2  

Thanks Stephen.

谢谢Stephen。

As a note, if you are using Windows Authentication, the @loginname is YourDomain\YourLogin (probably obvious to everybody else, but took me a couple tries.

请注意,如果您使用的是Windows身份验证,则@ login名称是YourDomain \ YourLogin(对其他人来说可能很明显,但是我尝试了几次。

sp_defaultdb @loginame='YourDomain\YourLogin', @defdb='YourDatabase'

#4


1  

If you're trying to change which database you are using after you are logged in, you can use the USE command. E.g. USE Northwind.

如果您在登录后尝试更改正在使用的数据库,则可以使用USE命令。例如。使用罗斯文。

https://www.tutorialspoint.com/sql/sql-select-database.htm

https://www.tutorialspoint.com/sql/sql-select-database.htm

#1


14  

from: http://doc.ddart.net/mssql/sql70/sp_da-di_6.htm

来自:http://doc.ddart.net/mssql/sql70/sp_da-di_6.htm

sp_defaultdb [@loginame =] 'login' , [@defdb =] 'database'

#2


30  

ALTER LOGIN should be used for SQL Server 2005 or later:

ALTER LOGIN应该用于SQL Server 2005或更高版本:

http://technet.microsoft.com/en-us/library/ms189828.aspx

http://technet.microsoft.com/en-us/library/ms189828.aspx

ALTER LOGIN <login_name> WITH DEFAULT_DATABASE = <default_database>

sp_defaultdb eventually will be removed from SQL Server:

最终将从SQL Server中删除sp_defaultdb:

http://technet.microsoft.com/en-us/library/ms181738.aspx

http://technet.microsoft.com/en-us/library/ms181738.aspx

#3


2  

Thanks Stephen.

谢谢Stephen。

As a note, if you are using Windows Authentication, the @loginname is YourDomain\YourLogin (probably obvious to everybody else, but took me a couple tries.

请注意,如果您使用的是Windows身份验证,则@ login名称是YourDomain \ YourLogin(对其他人来说可能很明显,但是我尝试了几次。

sp_defaultdb @loginame='YourDomain\YourLogin', @defdb='YourDatabase'

#4


1  

If you're trying to change which database you are using after you are logged in, you can use the USE command. E.g. USE Northwind.

如果您在登录后尝试更改正在使用的数据库,则可以使用USE命令。例如。使用罗斯文。

https://www.tutorialspoint.com/sql/sql-select-database.htm

https://www.tutorialspoint.com/sql/sql-select-database.htm