Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert or Update SQL Table with DataTable

Firstly, I can't use any stored procedures or views.
I know this may seem counter-productive, but those aren't my rules.

I have a DataTable, filled with data. It has a replicated structure to my SQL table.

ID - NAME

The SQL table currently has a bit of data, but I need to now update it with all the data of my DataTable. It needs to UPDATE the SQl Table if the ID's match, or ADD to the list where it's unique.

Is there any way to simply do this only in my WinForm Application?

So far, I have:

SqlConnection sqlConn = new SqlConnection(ConnectionString);
            SqlDataAdapter adapter = new SqlDataAdapter(string.Format("SELECT * FROM {0}", cmboTableOne.SelectedItem), sqlConn);
            using (new SqlCommandBuilder(adapter))
            {
                try
                {
                    adapter.Fill(DtPrimary);
                    sqlConn.Open();
                    adapter.Update(DtPrimary);
                    sqlConn.Close();
                }
                catch (Exception es)
                {
                    MessageBox.Show(es.Message, @"SQL Connection", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }

DataTable:

        DataTable dtPrimary = new DataTable();

        dtPrimary.Columns.Add("pv_id");
        dtPrimary.Columns.Add("pv_name");

        foreach (KeyValuePair<int, string> valuePair in primaryList)
        {
            DataRow dataRow = dtPrimary.NewRow();

            dataRow["pv_id"] = valuePair.Key;
            dataRow["pv_name"] = valuePair.Value;

            dtPrimary.Rows.Add(dataRow);

SQL:

CREATE TABLE [dbo].[ice_provinces](
    [pv_id] [int] IDENTITY(1,1) NOT NULL,
    [pv_name] [nvarchar](50) NOT NULL,
 CONSTRAINT [PK_ice_provinces] PRIMARY KEY CLUSTERED 
(
    [pv_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
like image 404
TheGeekZn Avatar asked Jul 23 '13 14:07

TheGeekZn


People also ask

What is the difference between INSERT and UPDATE?

Insert is for adding data to the table, update is for updating data that is already in the table.

Can we pass DataTable to a stored procedure?

We can pass the DataTable to the Stored Procedure using ADO.Net in the same way as we provided using the System. Data. SqlParameter class, but needs a few changes in the datatype. Normally we provide DbType of SqlParameter for a normal parameter like varchar, nvarchar, int and so on as in the following code.

Can INSERT or UPDATE be performed on simple views?

We can insert, update and delete data from simple view only when we have primary key and all not null fields are in view. We cannot insert/delete data in complex view we can only update it. In SQL, a view is a virtual table based on the result-set of an SQL statement.


1 Answers

Since you're going update existing values and insert new ones from datatable that has ALL the data anyway I think the following approach might work best for you:

  1. Delete all existing data from the SQL Table (you can use TSQL TRUNCATE statement for speed and efficiency
  2. Use ADO.NET SqlBulcCopy class to bulk insert data from ADO.NET table to SQL table using WriteToServer method.

No views or stored procedures involved, just pure TSQL and .NET code.

like image 166
Yuriy Galanter Avatar answered Oct 03 '22 15:10

Yuriy Galanter