Mega Code Archive

 
Categories / C# Tutorial / ADO Net
 

Execute update statement with SqlCommand against MDF file

using System; using System.Data;             using System.Data.SqlClient;   using System.Collections.Generic; using System.Text;   class Program   {     static void Main(string[] args)     {       SqlConnection thisConnection = new SqlConnection(                 @"Data Source=.\SQLEXPRESS;" +                 @"AttachDbFilename='NORTHWND.MDF';" +                 @"Integrated Security=True;Connect Timeout=30;User Instance=true");             thisConnection.Open();       SqlCommand thisCommand = thisConnection.CreateCommand();       thisCommand.CommandText = "UPDATE Products SET UnitPrice=1";       int rowsAffected = thisCommand.ExecuteNonQuery();       Console.WriteLine("Rows Updated = {0}", rowsAffected);       thisConnection.Close();     }   }