Sajo
Member
- Joined
- Jul 22, 2020
- Messages
- 17
- Programming Experience
- Beginner
I want to create a connection to the server. The base is called NORTHWND. I wanted to create a connection using a configuration file. I created a configuration file. I want to get information about the version of the server I am using through the console application. However when I run the code I only get the following. It seems that I can´t log into database.
Here is the main code and code of config file
Main code
Here is the code of configuration file
Here is the main code and code of config file
Main code
Main Code:
using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Data.SqlClient;
using System.Configuration;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = ConfigurationManager.ConnectionStrings["Northwind"].ConnectionString;
try
{
using (conn)
{
// Try to open the connection.
conn.Open();
Console.WriteLine("Server Version: " + conn.ServerVersion);
}
}
catch (Exception err)
{
// Handle an error by displaying the information.
Console.WriteLine("Error reading the database. ");
Console.WriteLine(err.Message);
}
conn.Close();
Console.WriteLine("Now Connection Is:" + conn.State.ToString());
}
}
}
Here is the code of configuration file
Config file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="Northwind" connectionString="Data Source=localhost;Initial Catalog=NORTHWND;Integrated Security=SSPI"/>
</connectionStrings>
</configuration>