M
M
Matvey Kremnin2016-12-01 21:34:43
C++ / C#
Matvey Kremnin, 2016-12-01 21:34:43

I can not understand what is the error in the SQL query?

I can't figure it out, it throws an error
An error occurred with the network or with a particular instance while establishing a connection to SQL Server. Server not found or unavailable. Make sure the instance name is correct and that remote connections are allowed on SQL Server. (provider: SQL Network Interfaces, error: 26 - An error occurred while finding the specified server or instance
, this is not an error, it is just a warning

class Program
    {
        static void Main(string[] args)
        {
            string connectionString = @"Data Source=.\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True";

            string sqlExpression = "SELECT * FROM Users";
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();
                SqlCommand command = new SqlCommand(sqlExpression, connection);
                SqlDataReader reader = command.ExecuteReader();

                if (reader.HasRows) // если есть данные
                {
                    // выводим названия столбцов
                    Console.WriteLine("{0}\t{1}\t{2}", reader.GetName(0), reader.GetName(1), reader.GetName(2));

                    while (reader.Read()) // построчно считываем данные
                    {
                        object id = reader.GetValue(0);
                        object name = reader.GetValue(1);
                        object age = reader.GetValue(2);

                        Console.WriteLine("{0} \t{1} \t{2}", id, name, age);
                    }
                }

                reader.Close();
            }

            Console.Read();
        }
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Konstantin Tsvetkov, 2016-12-02
@tsklab

connectionString
Testing and tuning is best done in a UDL file. Make a file, for example, test.udl (0 bytes) and click on it. The Data Link Properties dialog opens. OK and the correct connectionString will be written in the test.udl file.
[oledb]
; Everything after this line is an OLE DB initstring
Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=Phondex;Data Source=Phondex

e83c619693264be888f759b279ca83ad.png

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question