简体   繁体   English

如何在Visual Studio 2015中使用C#创建到SQL Server数据库的连接字符串

[英]How to create a connection string using C# to SQL Server database in Visual Studio 2015

I have a database for user details in my SQL Server and I have already written a C# login application. 我在SQL Server中有一个用于存储用户详细信息的数据库,并且已经编写了C#登录应用程序。 I need to connect this application to my database to get user data. 我需要将此应用程序连接到数据库以获取用户数据。 Before that, I think I should connect my database to Visual Studio using a connection string. 在此之前,我认为我应该使用连接字符串将数据库连接到Visual Studio。

Can anyone please tell me a way of creating a connection string to connect my database to the application? 谁能告诉我一种创建连接字符串以将数据库连接到应用程序的方法吗?

If you don't have an app.config file just add it by right click your project > add > new item > search box type application then select Application Configuration File then click Add button below. 如果您没有app.config文件,只需通过右键单击您的项目>添加>新建项目>搜索框类型应用程序将其Add然后选择Application Configuration File然后单击下面的Add按钮。 then open the App.config file and paste the code below. 然后打开App.config文件并paste以下代码。

It will be has a name of App.config in your project. 在您的项目中它将具有App.config的名称。

<configuration>
       <connectionStrings>
    <clear />
     <add name="conn" providerName="MySql.Data.MySqlClient" connectionString="data source=localhost;initial catalog=YourDBName;user=YourUsername;password=YourPassword; default command timeout=120;;" />
  </connectionStrings>

</configuration>

See this reference to run the code. 请参阅此参考以运行代码。

Good Luck! 祝好运!

You can use EntityFramework database first convention. 您可以使用EntityFramework 数据库的第一个约定。 The easiest way is to add ADO.NET Entity Data Model from visual studio Project -> Add New Item -> Data and then wizard helps you to connect to your database and creates required objects for tables in code 最简单的方法是从Visual Studio项目->添加新项->数据添加ADO.NET实体数据模型 ,然后向导帮助您连接到数据库并为代码中的表创建所需的对象

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Visual Studio 2015 中使用 SQL 服务器数据库从 C# 项目创建安装文件 - Create setup file from C# project with SQL server database in Visual Studio 2015 如何在Visual Studio 2015中为SQL Server创建数据库关系图? - How to create a database diagram for SQL Server in Visual Studio 2015? 在Visual Studio 2012 C#中连接到SQL Server数据库 - Connection to SQL Server Database inside Visual Studio 2012 C# 如何使用 SQL Server 作为数据库在 Visual Studio C# 中创建销售按钮代码 - How to Create a Sell Button Code In Visual Studio C# with SQL Server as database 如何将在 Visual Studio C# 中创建的数据库迁移到 SQL 服务器? - How to migrate a DataBase created in Visual Studio C# to a SQL Server? 如何在Visual Studio 2017中使用C#建立SQL连接? - how to build a SQL connection using C# in Visual Studio 2017? 使用 SqlTransaction 将日历选择器中的日期存储到 SQL Server 中 - Visual Studio 2015/C# - Store date from calendar picker into SQL Server using SqlTransaction - Visual studio 2015/C# 如何在Visual Studio 2015 / C#中使用本地SQL数据库引用子项目 - How to reference sub project with local SQL database in Visual Studio 2015/ C# 与Visual Studio 2015中的SQL Server数据库进行交互 - Interacting with SQL Server database in Visual Studio 2015 如何在visual studio 2015中创建C#类图表/ - How to create a C# class diagram in visual studio 2015/
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM