[英]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.