简体   繁体   English

在Visual Studio 2015中连接到数据库

[英]Connect to database in Visual Studio 2015

This is going to be simple/stupid question. 这将是一个简单/愚蠢的问题。

I have installed Visual studio 2015 Enterprise and created a class Library project with Framework 4.6. 我安装了Visual Studio 2015 Enterprise并使用Framework 4.6创建了一个类库项目。 But I am not able to find [System.Data]. 但我无法找到[System.Data]。

I want to connect to SQL server using SqlConnection and SqlCommand like object. 我想使用SqlConnection和类似对象的SqlCommand连接到SQL服务器。 IntelliSense is not working. IntelliSense无法正常工作。 I am not able to find example on Google. 我无法在Google上找到示例。 Every example I have found is related to framework 4.0., but I am used to working with Visual Studio 2010. 我发现的每个例子都与框架4.0有关,但我习惯使用Visual Studio 2010。

在此输入图像描述

Update: ooops 更新:ooops
Friends, I found that it was it was using project web-->Class library when I tried Windows-->Class Library project, it was all there. 朋友们,我发现它是在使用项目网 - >类库时我尝试了Windows - >类库项目,它就在那里。 But still I want to know the difference.... Now another problem is that I am not able to add reference this project in web project. 但我仍然想知道差异......现在另一个问题是我无法在web项目中添加引用这个项目。 It is giving some Dependency NewBLL>=1.0.0-* could not be resolved. 它给出了一些依赖性NewBLL> = 1.0.0- *无法解决。

The project template for a class library adds a reference to System.Data automatically. 类库的项目模板自动添加对System.Data的引用。

Try adding the following statement to the top of the class file: 尝试将以下语句添加到类文件的顶部:

using System.Data.SqlClient;

Answering this seems foolish as there are a plethora of examples from just a simple Google search... maybe I can help you along your way. 回答这个似乎是愚蠢的,因为只有一个简单的谷歌搜索有很多例子...也许我可以帮助你一路走来。 Please note this question will most likely be down-voted for duplication. 请注意,这个问题很可能会被重复投票。

For this particular scenario you are wanting to utilize SqlClient which as previously mentioned lives under System.Data which fully qualifies to System.Data.SqlClient . 对于这种特殊的情况下,您是想使用SqlClient如前面提到的在生活System.Data这完全有资格System.Data.SqlClient

using System.Data.SqlClient;

Once you have imported the namespace (not required, just prevents you from having to fully qualify everything) you may start working with the classes contained within. 导入命名空间后(不是必需的,只是阻止您完全限定所有内容),您可以开始使用其中包含的类。 The two we need to focus on are SqlConnection and SqlCommand . 我们需要关注的两个是SqlConnectionSqlCommand

SqlConnection is the class actually used to connect to a particular database. SqlConnection是实际用于连接到特定数据库的类。 It has several different constructors, but you may instantiate a new instance like this utilizing the default constructor: 它有几个不同的构造函数,但您可以使用默认构造函数实例化这样的新实例:

SqlConnection connection = new SqlConnection();

It is more common to provide the connection string within the constructor, but you may do so afterwards through the ConnectionString property. 在构造函数中提供连接字符串更为常见,但之后可以通过ConnectionString属性执行此操作。 Now digressing just slightly we should wrap the object in a using statement to prevent unmanaged resources from hanging around in memory: 现在稍微离题我们应该将object包装在using语句中,以防止非托管资源在内存中闲置:

using(SqlConnection connection = new SqlConnection()) 
{

} //<--object is disposed of at the end of the code block.

Read about the using statement https://msdn.microsoft.com/en-us/library/yh598w02.aspx and you may also look at IDisposable https://msdn.microsoft.com/en-us/library/system.idisposable(v=vs.110).aspx if necessary. 阅读using声明https://msdn.microsoft.com/en-us/library/yh598w02.aspx ,您也可以查看IDisposable https://msdn.microsoft.com/en-us/library/system.idisposable (v = vs.110).aspx如有必要。

Once you have a connection you may now instantiate a SqlCommand object using the same... well using statement: 一旦建立连接,您现在可以使用相同的... well using语句实例化SqlCommand object

using(SqlConnection connection = new SqlConnection())
{
    using(SqlCommand command = new SqlCommand())
    {

    }
}

Usually with SqlCommand you would supply the connection to it as well as command text to query against the SQL Server database. 通常使用SqlCommand您将提供与它的连接以及命令文本以查询SQL Server数据库。 Finally you execute the command with: 最后,您执行以下命令:

command.ExecuteNonQuery();

Please understand that I have purposely left out some of the details to allow you to research each individual class and obtain a more thorough understanding of what needs to be done. 请理解我故意遗漏了一些细节,以便您可以研究每个班级,并对需要完成的工作有更透彻的了解。

SqlConnection Class https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection(v=vs.110).aspx SqlConnection类https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection(v=vs.110).aspx

SqlCommand Class https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand(v=vs.110).aspx SqlCommand类https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand(v=vs.110).aspx

ConnectionString Property https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.connectionstring(v=vs.110).aspx ConnectionString属性https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.connectionstring(v=vs.110).aspx

Make sure you add normal class Library not class Library (package) 确保添加普通类库而不是类库(包)

类库

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

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