NonQueryTest.cs


Dosyayı İndir
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace DatabaseProject
{
  public class NonQueryTest
  {
    public void TestInsert()
    {
      string connectionString = "server=localhost;DataBase=CourseDatabase;uid=sa;pwd=godoro";
      SqlConnection connection = new SqlConnection(connectionString);
      connection.Open();

      String sql = "insert into Student(FirstName,LastName,BirthDate,IsGraduated,BodyWeight,GradeNumber)"
        +" Values('Orhan','Gencebay','05/04/1955',1,65,3)";
      SqlCommand command = new SqlCommand(sql, connection);
      int affectedRowCount = command.ExecuteNonQuery();
      Console.WriteLine("Affected Row Count: " + affectedRowCount);

      connection.Close();
    }
    public void TestInsertVariable()
    {
      string connectionString = "server=localhost;DataBase=CourseDatabase;uid=sa;pwd=godoro";
      SqlConnection connection = new SqlConnection(connectionString);
      connection.Open();

      string firstName = "Müslüm";
      string lastName = "Gürses";
      DateTime birthDate = new DateTime(1965, 4, 6);
      int isGraduated = 1;
      double bodyWeight = 67.5;
      int gradeNumber = 5;

      String sql = "insert into Student(FirstName,LastName,BirthDate,IsGraduated,BodyWeight,GradeNumber)"
        + " Values('" + firstName + "','" + lastName + "','" + birthDate + "'"
        + "," + isGraduated + "," + bodyWeight + "," + gradeNumber + ")";
      SqlCommand command = new SqlCommand(sql, connection);
      int affectedRowCount = command.ExecuteNonQuery();
      Console.WriteLine("Affected Row Count: " + affectedRowCount);

      connection.Close();
    }
    public void TestInsertParametric()
    {
      string connectionString = "server=localhost;DataBase=CourseDatabase;uid=sa;pwd=godoro";
      SqlConnection connection = new SqlConnection(connectionString);
      connection.Open();

      String sql = "insert into Student(FirstName,LastName,BirthDate,IsGraduated,BodyWeight,GradeNumber)"
        + " Values(@FirstName,@LastName,@BirthDate,@IsGraduated,@BodyWeight,@GradeNumber)";
      SqlCommand command = new SqlCommand(sql, connection);
      command.Parameters.AddWithValue("@FirstName", "Mahsuni");
      command.Parameters.AddWithValue("@LastName", "Şerif");
      command.Parameters.AddWithValue("@BirthDate", new DateTime(1955,4,7));
      command.Parameters.AddWithValue("@IsGraduated", 0);
      command.Parameters.AddWithValue("@BodyWeight", 76.4);
      command.Parameters.AddWithValue("@GradeNumber", 6);
      int affectedRowCount = command.ExecuteNonQuery();
      Console.WriteLine("Affected Row Count: " + affectedRowCount);

      connection.Close();
    }

    public void TestUpdate()
    {
      string connectionString = "server=localhost;DataBase=CourseDatabase;uid=sa;pwd=godoro";
      SqlConnection connection = new SqlConnection(connectionString);
      connection.Open();

      String sql = "update Student set FirstName='Ferdi',LastName='Tayfur' where StudentId=1";

      SqlCommand command = new SqlCommand(sql, connection);
      int affectedRowCount = command.ExecuteNonQuery();
      Console.WriteLine("Affected Row Count: " + affectedRowCount);

      connection.Close();
    }
    public void TestDelete()
    {
      string connectionString = "server=localhost;DataBase=CourseDatabase;uid=sa;pwd=godoro";
      SqlConnection connection = new SqlConnection(connectionString);
      connection.Open();

      String sql = "delete from Student where StudentId=1";

      SqlCommand command = new SqlCommand(sql, connection);
      int affectedRowCount = command.ExecuteNonQuery();
      Console.WriteLine("Affected Row Count: " + affectedRowCount);

      connection.Close();
    }
  }
}

				
Dosyayı İndir

Bu Sayfayı Paylaş:




Bu Sayfayı Paylaş:

İletişim Bilgileri

Takip Et

Her Hakkı Saklıdır. Bu sitede yayınlanan tüm bilgi ve fikirlerin kullanımından fibiler.com sorumlu değildir. Bu sitede üretilmiş , derlenmiş içerikleri, fibiler.com'u kaynak göstermek koşuluyla kendi sitenizde kullanılabilirsiniz. Ancak telif hakkı olan içeriklerin hakları sahiplerine aittir