/*
CREATE TABLE MyTable(
Id int NOT NULL PRIMARY KEY,
Field1 nvarchar(50) NOT NULL,
Field2 nvarchar(50) NOT NULL,
)
INSERT INTO MyTable VALUES (1, 'Field 1.1', 'Field 2.1');
INSERT INTO MyTable VALUES (2, 'Field 1.2', 'Field 2.2');
INSERT INTO MyTable VALUES (3, 'Field 1.3', 'Field 2.3');
*/
using System;
using System.Data;
using System.Data.SqlClient;
class Program
{
static void Main(string[] args)
{
string sqlConnectString = "Data Source=(local);Integrated security=SSPI;Initial Catalog=MyDatabase;";
string sqlDelete = "DELETE FROM MyTable WHERE Id = 2";
SqlConnection connection = new SqlConnection(sqlConnectString);
SqlCommand command = new SqlCommand(sqlDelete, connection);
connection.Open( );
int rowsAffected = command.ExecuteNonQuery( );
Console.WriteLine("{0} row(s) affected.", rowsAffected);
Console.WriteLine("Record with Id = 2 deleted.");
connection.Close( );
}
}
|