我已经用SQL Server Management Studio创建了一个数据库,我现在想在我的c#应用程序中使用它。我需要连接字符串?

我可以在哪里找到连接字符串,我的数据库存储在哪里?

我需要发表它吗,还是在我的文档里?

using (var conn = new SqlConnection("your connection string to the database"))

如何获取连接字符串?我在哪里可以找到连接字符串复制粘贴到上面的部分?

我如何发布我的数据库,以便Visual Studio可以选择它?然后我就可以拉动这里的连接线了?


当前回答

我的解决方案是使用excel(2010)。

在一个新的工作表中,选择一个单元格,然后:

Data -> From Other Sources -> From SQL Server 

输入服务器名,选择表,等等,

当你进入“导入数据”对话框时, 点击“连接属性”对话框中的属性, 选择“定义”页签。

Excel很好地显示了用于复制的连接字符串 (甚至导出连接文件…)

其他回答

如果您在项目中创建了连接管理器,那么您可以简单地从那里提取连接字符串。

String connection = this.dts.connections["<connection_manager_name>"];

在下面使用这个连接:

using (var conn = new SqlConnection(connection))

如果我说错了,请指正。

我的解决方案是使用excel(2010)。

在一个新的工作表中,选择一个单元格,然后:

Data -> From Other Sources -> From SQL Server 

输入服务器名,选择表,等等,

当你进入“导入数据”对话框时, 点击“连接属性”对话框中的属性, 选择“定义”页签。

Excel很好地显示了用于复制的连接字符串 (甚至导出连接文件…)

打开SQL Server Management Studio并运行以下查询。你会得到连接字符串:

select
    'data source=' + @@servername +
    ';initial catalog=' + db_name() +
    case type_desc
        when 'WINDOWS_LOGIN' 
            then ';trusted_connection=true'
        else
            ';user id=' + suser_name() + ';password=<<YourPassword>>'
    end
    as ConnectionString
from sys.server_principals
where name = suser_name()

朋友们,最简单的方法是在visual studio 2019上打开服务器资源管理器选项卡(在我的情况下),然后尝试创建到数据库的连接。创建成功连接后,只需右键单击它并进入属性。在那里,您将找到一个语法正确的字符串连接字段!…这对我来说很有效,因为我事先知道服务器的名称....只是无法找出正确的语法来运行我的ef脚手架…

如果您已经安装并设置了MS SQL Server和Management Studio,请转到Visual Studio (Visual Studio而不是SQL Server Management Studio)。

1] In Visual Studio go to Tools -> Connect to Database. 2] Under Server Name Select your Database Server Name (Let the list Populate if its taking time). 3] Under Connect to a Database, Select Select or enter a database name. 4] Select your Database from Dropdown. 5] After selecting Database try Test Connection. 6] If Test Connection Succeeds, Click Ok. 7] In Visual Studio go to View -> Server Explorer. 8] In Server Explorer window, Under Data Connections Select your Database. Right Click your Database -> Click Properties. 9] In Properties window you will see your Connection String.