

Once the Database activities is over, Connection should be closed and release the Data Source resources. When the connection is established, SQL Commands will execute with the help of the Connection Object and retrieve or manipulate the data in the database.


If you have a named instance of SQL Server, you'll need to add that as well. Initial Catalog=DatabaseName User ID=UserName Password=Password" The SqlConnection instance takes Connection String as argument and pass the value to the Constructor statement. An instance of the SqlConnection class in C# is supported the Data Provider for SQL Server Database. The SqlConnection Object is Handling the part of physical communication between the C# application and the SQL Server Database. The first step in a C# application is to create an instance of the Server object and to establish its connection to an instance of Microsoft SQL Server. NET Framework Data Provider for SQL Server. In the near future, README file will cover connections to other kinds of servers and databases.You can connect your C# application to data in a SQL Server database using the. Command and Data Reader SqlCommand cmd = new SqlCommand() cmd.Connection = conn cmd.CommandType = CommandType.Text cmd.CommandText = "SELECT * FROM Person.Person" SqlDataReader sdr = cmd.ExecuteReader(CommandBehavior.Default) Stephen GlasskeysCįinally, close and dispose of the objects when finished. This source creates a SQLcommand using the conn connection object created above. This is the connection string name, and we can use this to create a connection object in source: using System.Configuration //Retrieve Connection String By Name string sConn = "" ConnectionStringSettings settings = ConfigurationManager.ConnectionStrings SqlConnection conn = new SqlConnection(settings.ConnectionString) Stephen GlasskeysĪlternatively, you can access the same setting value, reading the Application's properties: string sConnection = Stephen GlasskeysĪnd of course, the connection can be used to create SqlCommand or other data objects like a SqlDataReader. Notice the name=" " entry in the example image below. Look for values inside the connectionStrings tag. Next, open app.config to examine its contents.

When the project is saved, Visual Studio updates setting information in the app.config XML file. However if everything works as it should, don't forget to click the Save toolbar icon to keep your new connection string setting. If you click Test Connection again and receive another error message, you may wish to read my piece on How to fix common SQL Server Management Studio 'Connect to Server' errors. To validate your connection, use the Test Connection feature, and click OK if you receive a "test connection succeeded" notification.īut.you may get an error message, which means you'll probably need to double-check the server name, log on credentials, or authentication entries.
