Strange that solution1 does not work for you, please try again from scratch, maybe you have set some properties that cause problems.
Here is my complete example for SQL Server Express with Windows Authentication, it works without any problems on my Windows 7 notebook with SQL Server 2008 R2 and .NET 4.5.
The Chinook database is an example database which can be downloaded from CodePlex: https://chinookdatabase.codeplex.com/
using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace TestForm1
{
public partial class Form6 : Form
{
string connectionString = @"Integrated Security=SSPI;Persist Security Info=False;Data Source=(local)\SQLEXPRESS;Initial Catalog=Chinook";
DataTable dataTable;
public Form6()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
bool result;
using (var con = new SqlConnection(connectionString))
{
con.Open();
result = con.State == System.Data.ConnectionState.Open;
if (!result)
{
MessageBox.Show("Could not connect !");
}
else
{
Console.WriteLine("SQL Server Express " + con.ServerVersion);
SqlCommand cm = new SqlCommand("Select * from Artist", con);
SqlDataReader dr;
dr = cm.ExecuteReader();
dataTable = new DataTable();
dataTable.Load(dr);
dataGridView1.DataSource = dataTable;
}
}
}
private void buttonSave_Click(object sender, EventArgs e)
{
for (int i = 0; i < this.dataTable.Rows.Count; i++)
{
string query = string.Empty;
int result;
switch (this.dataTable.Rows[i].RowState)
{
case DataRowState.Added:
Debug.Print("Row " + i + " state = added");
query = "INSERT INTO Artist (ArtistId, Name) VALUES(@ArtistId, @Name)";
break;
case DataRowState.Deleted:
Debug.Print("Row " + i + " state = deleted");
break;
case DataRowState.Detached:
Debug.Print("Row " + i + " state = detached");
break;
case DataRowState.Modified:
Debug.Print("Row " + i + " state = modified");
query = "UPDATE Artist SET Name=@Name WHERE ArtistId=@ArtistId";
break;
}
if (!string.IsNullOrEmpty(query))
{
using (var con = new SqlConnection(connectionString))
{
con.Open();
using (SqlCommand command = new SqlCommand(query, con))
{
Debug.Print("ArtistId = " + this.dataTable.Rows[i].ItemArray[0]);
Debug.Print("Name = " + this.dataTable.Rows[i].ItemArray[1]);
command.Parameters.AddWithValue("@ArtistId", this.dataTable.Rows[i].ItemArray[0]);
command.Parameters.AddWithValue("@Name", this.dataTable.Rows[i].ItemArray[1]);
result = command.ExecuteNonQuery();
Debug.Print("result = " + result);
}
}
}
}
}
}
}