C#
В файле Program.cs напишем код:
using System;
// подключаем SQL ADO.NET
using Microsoft.Data.SqlClient;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// connection string
string connectionString = @"Data Source=EVGENI; Initial Catalog=MyDatabase1; Integrated Security=True";
// connect to db
using (SqlConnection connection = new SqlConnection(connectionString))
{
// open db
connection.Open();
// вызываем хранимую процедуру "GetBooks"
using (SqlCommand command = new SqlCommand("GetBooks", connection))
{
// Тип команды это хранимая процедура
command.CommandType = System.Data.CommandType.StoredProcedure;
SqlDataReader reader = command.ExecuteReader();
// есть ли данные
if (reader.HasRows)
{
// построчно считываем данные
while (reader.Read())
{
int id = reader.GetInt32(0); // Id
string name = reader.GetString(1); // Name
int price = reader.GetInt32(2); // Price
// object price = reader.GetValue(2); // получаем результат как object
// выводим на экран
Console.WriteLine("Id={0}, Name={1}, Price={2}", id, name, price);
}
}
}
}
}
}
}