0

I am using MySql 5.6 Server, client, C# Connector and Workbench on a Ubuntu 16.04 Lenovo ThinkStation desktop. I am experiencing a problem with the following stored procedure shown below when it is programmatically invoked with the MySQL C# Connector API:

CREATE PROCEDURE spAddInto_TableOne_and_TableTwo(
     IN now datetime not null,
     IN quantity int not null,
     IN desc nvarchar(32) not null)
   BEGIN

     call spAddInto_TableOne(now,quantity,desc);
     call spAddInto_TableTwo(now,quantity,desc);

   END;

where MySql TableOne looks like this:

CREATE TABLE TableOne(
EventDateTime datetime NOT NULL, 
Quantity smallint NULL,
Description nvarchar(32) NULL,
ID int AUTO_INCREMENT NOT NULL,
PRIMARY KEY (ID)
)

and Table 2 looks like this:

CREATE TABLE TableOne(
EventDateTime datetime NOT NULL, 
Quantity smallint NULL,
Description nvarchar(32) NULL,
ID int AUTO_INCREMENT NOT NULL,
PRIMARY KEY (ID)
)

and

MySql stored procedures spAddInto_TableOne and spAddInto_TableTwo are very similar atomic insert statements using implicit transactions as shown here:

    INSERT INTO TableOne
           (    EventDateTime
           ,    Type
           ,    Description)
     VALUES
           (    _eventDateTime
           ,    _type
           ,    ltrim(rtrim(_description))) 

and

  INSERT INTO TableTwo
           (    EventDateTime
           ,    Type
           ,    Description)
     VALUES
           (    _eventDateTime
           ,    _type
           ,    ltrim(rtrim(_description))) 

With this background, here I state my problem which I was hoping an clever programmer could fix:

When the stored procedure , spAddInto_TableOne_and_TableTwo, is programmatically invoked with the MySQL C# Connector API, only TableTwo is inserted into and TableOne remains empty.

It is interesting to note that when I run spAddInto_TableOne_and_TableTwo in MySql Workbench, I noticed it functions okay by inserting a row into TableOne and TableTwo.

This is my C# MySql Connector framework code:

SqlConnection conn = new SqlConnection(MyConnectionString);

conn.Open();
SqlCommand cmd = new SqlCommand(sProc, conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("eventDateTime", eventDate);
cmd.Parameters.AddWithValue("eventtype", type);
cmd.Parameters.AddWithValue("description", desc.Trim())

cmd.ExecuteNonQuery();

Should I use C# MySqlConnection.BeginTransaction() and MySqlConnection.Commit() in the C# code directly above?

Any help is greatly appreciated.

10
  • I don't work with MySql but I do work with ADO.Net and I think you have to prefix your parameters with @ or ? even with MySql. Commented Jul 16, 2016 at 11:14
  • You are using the wrong classes, no? See what I answered Here Commented Jul 16, 2016 at 11:35
  • @Drew, Thank you for your comment. I am using the right MySQL Connector classes because I aliased MySQLConnection and MySQLCommand as SQLConnection and SQLCommand respectively. Commented Jul 16, 2016 at 12:08
  • bundle up a zip of the .cs and the .sql for create tables and I will test it Commented Jul 16, 2016 at 12:09
  • @Crowcoder, Thank you for your comment. You have to prefix your parameters with @ with Microsoft SQLServer. I did not preface MySQLParameter with ? or @. Commented Jul 16, 2016 at 12:17

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.