Well, I'm new to C # and then I'm seeing a few database connection tutorials that appear to me this error:
ThecodeI'vedoneisallstillbasedonabasictutorialandI'llleaveithereforyoutosee
using System;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace teste
{
class Program
{
static void Main(string[] args)
{
SqlConnection connection = new SqlConnection("Server=.\SQLEXPRESS;Database=teste; Integrated Security = true");
{
connection.Open();
//
// The SqlCommand should be created inside a using statement.
// ... It receives the SQL statement as the first argument.
// ... It receives the connection object as the second argument.
// ... The SQL text only works with a specific database.
//
using (SqlCommand command = new SqlCommand(
"SELECT TOP 3 * FROM my_cab",
connection))
{
//
// Instance methods can be used on the SqlCommand instance.
// ... These read data from executing the command.
//
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
for (int i = 0; i < reader.FieldCount; i++)
{
Console.WriteLine(reader.GetValue(i));
}
Console.WriteLine();
}
}
}
}
}
}
}