0

I trying to make a backend method call work which connects to database and executes the record in a table. The method call works perfectly and it is not throwing any error but at the same time it does not update any record in the table.

Note:

  1. SELECT query returns the data as expected.
  2. It is insert and update that does not work as expected.

Here is my method snippet which connects the database and tries to execute update query with ExecuteNonQuery() method.

    public void Update(Insights ins)
        {
            string DbConnection = ConnectionSetting.ConnectDatabricks();
            string query = @"UPDATE Archery SET Insight = '?', Title = '?' WHERE Sub_Topic = '?'";
            
            try
            {
                using (OdbcConnection connection = new OdbcConnection(DbConnection))
                {
                    connection.Open();
                    OdbcCommand command = new OdbcCommand(query, connection);
                    command.CommandText = query;

                    command.Parameters.Add("@insight", OdbcType.NVarChar).Value = ins.Insight;
                    command.Parameters.Add("@title", OdbcType.NVarChar).Value = ins.Title;
                    command.Parameters.Add("@subject", OdbcType.NVarChar).Value = ins.Subject;
                    command.ExecuteNonQuery();
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

The table schema is as below:


Column Name Type


InsightId BigInt (primary key)

Insight String

Title String

Subject String


Binoy
  • 390
  • 5
  • 19
  • Have you supplied the correct information? The `where` clause has `Sub_Topic`, but your parameter has `subject`. This isn't necessarily a problem unless the data doesn't match that which is in the database. – Tu deschizi eu inchid Apr 03 '23 at 17:16
  • It's not necessary to set `CommandText` twice - once in the constructor and again through the property. Also, according to [OdbcCommand.ExecuteNonQuery](https://learn.microsoft.com/en-us/dotnet/api/system.data.odbc.odbccommand.executenonquery?view=dotnet-plat-ext-7.0#returns): _For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command. For all other types of statements, the return value is -1_ – Tu deschizi eu inchid Apr 03 '23 at 17:18
  • Does table have a Primary Key? When you have a primary key update will only modify database if the key exists. If you are adding a new key, then use Insert. ExecuteNonQuery returns an integer indicating number of rows changed. If you are getting a zero then switch from Insert to Update, or Update to Insert. You will not get an error if you are using the wrong command. – jdweng Apr 03 '23 at 17:24
  • The following [post](https://stackoverflow.com/a/69638011/10024425) may be helpful - for _Select_, _Insert_, and _Update_ the usage of [ODBC](https://learn.microsoft.com/en-us/dotnet/api/system.data.odbc?view=dotnet-plat-ext-8.0) is similar to [OLEDB](https://learn.microsoft.com/en-us/dotnet/api/system.data.oledb?view=dotnet-plat-ext-7.0) – Tu deschizi eu inchid Apr 03 '23 at 18:20
  • Thanks guys. But I still have the same issue. One thing I forgot to put in my question that the reason I use ODBC is to access tables in Databricks. Again, data retrieval (SELECT queries) are fine it is just insert/update that is causing issues. It always returns -1. Yes the table has primary key already defined. And tried specifying in the query as well but no avail. – Binoy Apr 03 '23 at 19:46
  • Please add the table definition to your post. As someone already mentioned, _Insert_ is used if the record doesn't exist. _Update_ updates an existing record. – Tu deschizi eu inchid Apr 03 '23 at 20:01
  • I added the schema structure in my post. – Binoy Apr 04 '23 at 04:30

0 Answers0