Ahoj sem se zkousel pripojit k databzi kde sem dal nejakou vyjimku, kde mam potom umistit finaly pro uzavreni spojeni
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string source = @"Data Source = .\SQLEXPRESS;AttachDbFilename = |DataDirectory|\Database1.mdf;Integrated Security = True;User Instance = True";
string select = "SELECT Prace,Jmeno FROM tblKontakty";
SqlConnection sql = new SqlConnection(source);
try
{
sql.Open();
}
catch (SqlException e)
{
Console.WriteLine(e.Message);
}
finally
{
sql.Close();
}
SqlCommand cmd = new SqlCommand(select, sql);
SqlDataReader reader = null;
try
{
reader = cmd.ExecuteReader();
}
catch (SqlException e)
{
Console.WriteLine(e.Message);
}
while (reader.Read())
{
Console.WriteLine("Prace: {0} Jmeno: {1}",reader[0],reader[1]);
}
}
}
}