SQL Metodlar ve Sorgu Türleri

csharp sql

C# uygulamalarımızda veritabanıyla ilgili bir sorgu yazdırdığımızda bu sorguyu çalıştırmak için C# bize çeşitli metodlar sunmuştur. Bunlar :

ExecuteScalar : Sorgumuz scalar tek bir değer döndürüyorsa sorgumuzu bu metod ile çalıştırmakta sorgu hızı bakımından fayda vardır.

ExecuteNonQuery : Sorgumuzda UPDATE, DELETE gibi işlemler yapıyorsak bu metodu kullanmalıyız. Ayrıca bu metod ektilediği kayıt sayısını döndürür.

ExecuteReader : Eğer sorgumuzda bir tablo için SELECT işlemi yaptıysak SELECT attığımız kayıtları okumak için bu metodu kullanmalıyız.

ExecuteScalar kullanımı ile ilgili örnek

mySqlConnection.Open();
mySqlCommand.Connection = mySqlConnection;
mySqlCommand.CommandType = CommandType.Text;
mySqlCommand.CommandText = “SELECT COUNT(ID) FROM DENEMETABLE”;
int count = (int) mySqlCommand.ExecuteScalar();

ExecuteNonQuery kullanımı ile ilgili örnek

mySqlConnection.Open();
mySqlCommand.Connection = mySqlConnection;
mySqlCommand.CommandType = CommandType.Text;
mySqlCommand.CommandText = “UPDATE DENEMETABLE SET WEBSITE = ‘safakunel.blogspot.com’ WHERE ID = 1”;
mySqlCommand.ExecuteNonQuery();
int etkilenenKayitSayisi = mySqlCommand.ExecuteNonQuery();

ExecuteReader kullanımı ile ilgili örnek

mySqlConnection.Open();
mySqlCommand.Connection = mySqlConnection;
mySqlCommand.CommandType = CommandType.Text;
mySqlCommand.CommandText = “SELECT * FROM DENEMETABLE”;
SqlDataReader r = mySqlCommand.ExecuteReader();
while (r.Read())
{
//okunan satırla ilgili işlemler buraya yazılır.
}
r.Close();
mySqlConnection.Close();

C# – Read, Insert, Update, Delete From SQL Database

Code snippets for reading, inserting, updating and deleting from SQL database.

static void Read()
{
try
{
string connectionString =
"server=.;" +
"initial catalog=employee;" +
"user id=sa;" +
"password=sa123";
using (SqlConnection conn =
new SqlConnection(connectionString))
{
conn.Open();
using (SqlCommand cmd =
new SqlCommand("SELECT * FROM EmployeeDetails", conn))
{
SqlDataReader reader = cmd.ExecuteReader();

if (reader.HasRows)
{
while (reader.Read())
{
Console.WriteLine("Id = ", reader["Id"]);
Console.WriteLine("Name = ", reader["Name"]);
Console.WriteLine("Address = ", reader["Address"]);
}
}

reader.Close();
}
}
}
catch (SqlException ex)
{
//Log exception
//Display Error message
}
}

static void Insert()
{
try
{
string connectionString =
"server=.;" +
"initial catalog=employee;" +
"user id=sa;" +
"password=sa123";
using (SqlConnection conn =
new SqlConnection(connectionString))
{
conn.Open();
using (SqlCommand cmd =
new SqlCommand("INSERT INTO EmployeeDetails VALUES(" +
"@Id, @Name, @Address)", conn))
{
cmd.Parameters.AddWithValue("@Id", 1);
cmd.Parameters.AddWithValue("@Name", "Amal Hashim");
cmd.Parameters.AddWithValue("@Address", "Bangalore");

int rows = cmd.ExecuteNonQuery();

//rows number of record got inserted
}
}
}
catch (SqlException ex)
{
//Log exception
//Display Error message
}
}

static void Update()
{
try
{
string connectionString =
"server=.;" +
"initial catalog=employee;" +
"user id=sa;" +
"password=sa123";
using (SqlConnection conn =
new SqlConnection(connectionString))
{
conn.Open();
using (SqlCommand cmd =
new SqlCommand("UPDATE EmployeeDetails SET Name=@NewName, Address=@NewAddress" +
" WHERE Id=@Id", conn))
{
cmd.Parameters.AddWithValue("@Id", 1);
cmd.Parameters.AddWithValue("@Name", "Munna Hussain");
cmd.Parameters.AddWithValue("@Address", "Kerala");

int rows = cmd.ExecuteNonQuery();

//rows number of record got updated
}
}
}
catch (SqlException ex)
{
//Log exception
//Display Error message
}
}

static void Delete()
{
try
{
string connectionString =
"server=.;" +
"initial catalog=employee;" +
"user id=sa;" +
"password=sa123";
using (SqlConnection conn =
new SqlConnection(connectionString))
{
conn.Open();
using (SqlCommand cmd =
new SqlCommand("DELETE FROM EmployeeDetails " +
"WHERE Id=@Id", conn))
{
cmd.Parameters.AddWithValue("@Id", 1);

int rows = cmd.ExecuteNonQuery();

//rows number of record got deleted
}
}
}
catch (SqlException ex)
{
//Log exception
//Display Error message
}
}

Bir yanıt yazın