Your code works a 100% great, i modificate the code for use with stored procedure but it's the same look!
void insertardgv2()
{
SqlConnection con =
new SqlConnection("Server=mypc Database=AdventureWorks2012;Trusted_Connection=True;");
SqlCommand cmd = new SqlCommand("insertardatos", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@C1", SqlDbType.NVarChar));
cmd.Parameters.Add(new SqlParameter("@C2", SqlDbType.NVarChar));
con.Open();
foreach (DataGridViewRow row in dataGridView2.Rows)
{
if (!row.IsNewRow)
{
cmd.Parameters["@C1"].Value = row.Cells[0].Value;
cmd.Parameters["@C2"].Value = row.Cells[1].Value;
cmd.ExecuteNonQuery();
}
}
}
and here are my stored procedure in sql
create procedure insertardatos
(
@C1 nvarchar (40),
@C2 nvarchar (40)
)
as
INSERT INTO Contacts(CompanyName, Phone) VALUES (@C1, @C2);
go