This might work...but it's not the best programming practice
first create a storedprocedure that takes all field for your specific table but it's important that you declare them with a default values of null so that if you do not supply them you will not get an error...
create procedure spImportUsers
(
@Name nvarchar(20) = null
)
then in your button click event do something like this...
List<string> lstColumns = new List<string>();
foreach (DataColumn dc in dataGridView1.Columns)
lstColumns.Add(dc.ColumnName);
foreach (DataRow row in dataGridView1.Rows)
{
using (SqlConnection conn = new SqlConnection(YourConnectionString))
{
SqlCommand cmd = new SqlCommand("spImportUsers", conn);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
for (int i = 0; i < lstColumns.Count; i++)
cmd.Parameters.AddWithValue(lstColumns[i].ToString(), row[lstColumns[i].ToString()].ToString());
conn.Open();
cmd.ExecuteNonQuery();
}
}
This solution will open a connection for each row but it will be easy to create a solution where you send through a list of values, I will leave that up to you...
Also take note that this solution is aimed at you knowing which table is being imported but not knowing which columns will be supplied, also that the columns being supplied are only column that are actually in the table.
When importing multiple values it is always important to create a transaction so that when one fails all the values will be rolled back... If you do not know how to create a transaction c# side or sql side if you google it you will get some helpful links
really hopes this helps you...