Tom Knowlton
asked on
INSERT into SQL Server table using rows of a DataGridView - LINQ version in C#?
I want to do the same thing as the code below -- but using LINQ in C# ( LINQ to SQL ) to do the iterating and inserting. Please provide exact C# code. The broader question is how to do basic CRUD operations in LINQ. I do have a context called "Model1":
I want just very simple statements ... nothing complex. Just basic CRUD operations.
I managed to get a SELECT working .. but I am not sure how to do an INSERT or UPDATE.
I want just very simple statements ... nothing complex. Just basic CRUD operations.
I managed to get a SELECT working .. but I am not sure how to do an INSERT or UPDATE.
using (var db = new Model1())
{
//do a select
var query = from b in db.Customers
orderby b.CustID
select b;
// //Console.WriteLine("All blogs in the database:");
foreach (var item in query)
{
Console.WriteLine(item.CustID);
}
}
using (var db = new Model1())
{
// do the insert
}
using (var db = new Model1())
{
// do the update
}
using (var db = new Model1())
{
// do the delete
}
private void btnInsert_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
string constring = @"Data Source=.\SQL2008R2;Initial Catalog=AjaxSamples;Integrated Security=true";
using (SqlConnection con = new SqlConnection(constring))
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO Customers VALUES(@CustomerId, @Name, @Country)", con))
{
cmd.Parameters.AddWithValue("@CustomerId", row.Cells["Id"].Value);
cmd.Parameters.AddWithValue("@Name", row.Cells["Name"].Value);
cmd.Parameters.AddWithValue("@Country", row.Cells["Country"].Value);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
MessageBox.Show("Records inserted.");
}
What error are you getting.
ASKER
My question is not because of an error I am getting.
My question is about "How To" do CRUD operations in LINQ.
My question is about "How To" do CRUD operations in LINQ.
Hi Tom;
You state in your post the following, "LINQ in C# ( LINQ to SQL )", In your last question you where working with Linq to Entity Framework. Which technology is this question for, Linq to SQL ro Linq to Entity Framework.
Also initially will the DataGridView be loaded from the database or are you creating all the records in the DataGridView in the application and then update the database?
Also the database AjaxSamples is that a sample database you got off the web? If yes can you post the link.
You state in your post the following, "LINQ in C# ( LINQ to SQL )", In your last question you where working with Linq to Entity Framework. Which technology is this question for, Linq to SQL ro Linq to Entity Framework.
Also initially will the DataGridView be loaded from the database or are you creating all the records in the DataGridView in the application and then update the database?
Also the database AjaxSamples is that a sample database you got off the web? If yes can you post the link.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you!
Tom
Tom
Not a problem Tom, glad to help.
ASKER
; )