1

I try to make Sql connection using Ado.Net. I create a ConsoleApplication and get the Name and UnitPrice values from my database. After execution Console says can not open a connection. What is wrong that I make ?

Here is my code:

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

class ConsoleApplication1
{
    static void Main()
    {
        string connectionString =
            "Data Source=EMINCIFTCI/EMIN;Initial Catalog=Ado;User ID=sa;Password=10203040";


        string queryString =
            "SELECT Name, UnitPrice from dbo.Product "   
                + "ORDER BY UnitPrice DESC;";

        using (SqlConnection connection =
            new SqlConnection(connectionString))
        {

            SqlCommand command = new SqlCommand(queryString, connection);

            try
            {
                connection.Open();
                SqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    Console.WriteLine("\t{0}\t{1}",
                        reader[0], reader[1]);
                }
                reader.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.ReadLine();
        }
    }
}

My Database

6
  • Can you provide proper connection string from for example MS SQL Server Managment Studio ? Commented Dec 1, 2016 at 9:18
  • Add Error Message Commented Dec 1, 2016 at 9:21
  • Search the exception in google and you will find your problem. Commented Dec 1, 2016 at 9:21
  • My suggestion is Initial Catalog=Ado but most probably connection string issue Commented Dec 1, 2016 at 9:31
  • @Alexander Uploaded picture of my sql server management Commented Dec 1, 2016 at 10:12

2 Answers 2

3

Assuming EMINCIFTCI/EMIN is your computer name and (I assume) SQL Server instance, you need to swap the forward slash with a backslash (two, technically, unless you use a verbatim string).

So, use either

string connectionString =
            "Data Source=EMINCIFTCI\\EMIN;Initial Catalog=Ado;User ID=sa;Password=10203040";

or

string connectionString =
            @"Data Source=EMINCIFTCI\EMIN;Initial Catalog=Ado;User ID=sa;Password=10203040";

You may want to review https://www.connectionstrings.com/

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for helping. I made a mistake with data source. It is working now
0

I think the connection string should be proper"Data Source=EMINCIFTCI/EMIN;Initial Catalog=Ado;User ID=sa;Password=10203040;"

There should be semicolon in the end

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.