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

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

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

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

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

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


当前回答

如果使用Linqpad工具,从连接连接到目标数据库后,可以获得要使用的连接字符串。

右键单击数据库连接。 选择属性 选择先进的 选择将完整连接字符串复制到剪贴板

结果:数据源=.\jabberwocky;集成安全=SSPI;初始目录=Rasa;应用=LINQPad


删除应用=LinqPad取决于驱动程序和其他项目如服务器而不是源代码,您可能需要调整驱动程序以适应目标操作;但它给了一个发射台。

其他回答

如果您已经安装并设置了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.

检索连接字符串的一个非常简单的方法是创建一个文本文件,将扩展名从.txt更改为.udl。

双击.udl文件将打开Data Link Properties向导。

配置并测试到数据库服务器的连接。

关闭向导,并用您选择的文本编辑器打开.udl文件,并简单地复制连接字符串(不包含Provider=<driver>部分),以便在c#应用程序中使用它。

示例udl文件内容

[oledb]
; Everything after this line is an OLE DB initstring
Provider=SQLNCLI11.1;Integrated Security=SSPI;Persist Security Info=False;User ID="";Initial Catalog=YOURDATABASENAME;Data Source=YOURSERVERNAME;Initial File Name="";Server SPN=""

你需要从它复制什么

Integrated Security=SSPI;Initial Catalog=YOURDATABASENAME;Data Source=YOURSERVERNAME;

如果您想指定用户名和密码,您可以从其他答案中采用。

教程:https://teusje.wordpress.com/2012/02/21/how-to-test-an-sql-server-connection/

SqlConnection con = new SqlConnection();
con.ConnectionString="Data Source=DOTNET-PC\\SQLEXPRESS;Initial Catalog=apptivator;Integrated Security=True";

把下面的标签放在网上。配置节点中的配置文件

 <connectionStrings>
<add name="NameOFConnectionString" connectionString="Data Source=Server;Initial Catalog=DatabaseName;User ID=User;Password=Pwd"
  providerName="System.Data.SqlClient" />

然后你可以使用上面的连接字符串,例如:

SqlConnection con = new SqlConnection();
            con.ConnectionString = ConfigurationManager.ConnectionStrings["NameOFConnectionString"].ToString();

打开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()