[英]Connecting to server vs connecting to database
What's the difference between connecting to a server as opposed to connecting to a database? 连接服务器而不是连接数据库有什么区别?
The context of the question is that I'm in charge of developing a proof of concept where a user can select one of our servers, a database within that server, a table within that database, and a column within that table. 问题的背景是我负责开发一个概念证明,用户可以选择我们的服务器之一,该服务器中的数据库,该数据库中的表以及该表中的列。 I am using Visual C# and ASP.NET.
我正在使用Visual C#和ASP.NET。 I believe I can get the servers from the connection strings in the web.config, but I'm not quite sure how.
我相信我可以从web.config中的连接字符串中获取服务器,但我不太清楚如何。
If it helps at all (I do like examples), you can assume SQL servers. 如果它有帮助(我喜欢例子),你可以假设SQL服务器。
(Answer to original question) (回答原问题)
There is a hierarchy: 有一个层次结构:
(Answer to updated question) (回答更新的问题)
It is different per SQL provider, but using Microsleft SQL server you just connect to the server (don't provide a default instance in the connection string) and do the following: 每个SQL提供程序都不同,但使用Microsleft SQL服务器只需连接到服务器(不要在连接字符串中提供默认实例)并执行以下操作:
select * from sys.databases
Once you have your database, connect to that database and do the following to get the tables 获得数据库后,连接到该数据库并执行以下操作以获取表
select * from sys.tables where type = 'U'
to get the columns you do 获取你做的列
select * from sys.Columns
however to get the name of the table the column is in you need to match Object_id
to Object_id
on sys.tables
但是要获取列所在表的名称,需要将
Object_id
上的sys.tables
与Object_id
匹配
select t.name as TableName, c.Name as ColumnName
from sys.tables t
inner join sys.columns c on t.object_id = c.object_id
where t.Type = 'U'
You can achieve your goal. 你可以实现你的目标。 Initially, you would connect to database
master
on the server and query the databases on that server. 最初,您将连接到服务器上的数据库
master
服务器并查询该服务器上的数据库。
SELECT * FROM sys.databases
Then, you would initiate a new connection to the selected database and query that database's information schema, to get a list of tables. 然后,您将启动与所选数据库的新连接并查询该数据库的信息架构,以获取表列表。
SELECT * FROM INFORMATION_SCHEMA.TABLES
Repeat for selecting a column. 重复以选择列。
SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'foo'
如果服务器是指数据库服务器,则应连接到服务器以访问该服务器上托管的数据库。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.