简体   繁体   English

连接到服务器vs连接到数据库

[英]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: 有一个层次结构:

  • Server : A piece of physical (or virtual) hardware that runs an OS and applications. 服务器 :运行操作系统和应用程序的一块物理(或虚拟)硬件。 You will address it via IP address or DNS name, in can host multiple Database Servers 您将通过IP地址或DNS名称解决它,可以托管多个数据库服务器
    • Database Server (aka Instance) : A piece of software that runs that can host multiple Databases. 数据库服务器(又称实例) :运行可以托管多个数据库的软件。 when you use a connection string it is in the format "ServerName\\InstanceName" 当您使用连接字符串时,它的格式为“ServerName \\ InstanceName”
      • Database : A data structure that can host multiple Data Tables 数据库 :可以托管多个数据表的数据结构
        • Data Table : A data structure that can host multiple Columns and Rows 数据表 :可以托管多个列和行的数据结构
          • Column : The smallest division of information seperation, holds information about a specific topic :信息分离的最小部分,包含有关特定主题的信息
          • Row : Holds a single set of columns. :包含一组列。

(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.tablesObject_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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM