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. But I am not able to find [System.Data].
I want to connect to SQL server using SqlConnection
and SqlCommand
like object. IntelliSense is not working. I am not able to find example on Google. Every example I have found is related to framework 4.0., but I am used to working with Visual Studio 2010.
Update: 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. 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. It is giving some Dependency NewBLL>=1.0.0-* could not be resolved.
The project template for a class library adds a reference to System.Data
automatically.
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
.
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
.
SqlConnection
is the class actually used to connect to a particular database. 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. Now digressing just slightly we should wrap the object
in a using
statement to prevent unmanaged resources from hanging around in memory:
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.
Once you have a connection you may now instantiate a SqlCommand
object
using the same... well using
statement:
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. 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
SqlCommand Class 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.