0

I am trying to store a BigInteger as a DECIMAL(36,18) in C#. When I try to add the value like this:

public void AddTrade(BigInteger price) {
    _connection.Open();
    var command = new MySqlCommand(@"INSERT INTO Prices (Price) VALUES (@Price);", _connection);
    command.Parameters.Add("@Price", MySqlDbType.Decimal).Value = price;
    command.ExecuteNonQuery();
    _connection.Close();
}

I get the following exception:

System.NotSupportedException: 'Parameter type BigInteger is not supported; 

Here is how I created the table:

public void CreatePriceTable() {
    _connection.Open();
    var createTable = new MySqlCommand(
        @"
        CREATE TABLE `Prices` (
        Price DECIMAL(36,18) NOT NULL,
        ) COLLATE='utf8_general_ci' ENGINE=InnoDB;"
        , _connection);
    createTable.ExecuteNonQuery();
    _connection.Close();

}

I wasn't able to find the solution in any documentation and hope someone can help me with this. Thank you!

edit: The reason I need to use a BigInteger is because I am receiving a price in Wei (the smallest unit on the Ethereum blockchain) as BigInteger. One Ether is 1000000000000000000 Wei. Other posts suggested using DECIMAL(36,18) for Wei.

moccajoghurt
  • 159
  • 9
  • you can directly convert price to double – sercanD Oct 24 '21 at 13:23
  • 1
    Does this answer your question? [Fastest way to convert a BigInteger to a decimal (Base 10) string?](https://stackoverflow.com/questions/13154304/fastest-way-to-convert-a-biginteger-to-a-decimal-base-10-string) – Luuk Oct 24 '21 at 13:24
  • Most likely it expects C# 'decimal' type for column of type 'DECIMAL'. Not `BigInteger`. BigInteger has unlimited precision and in general case cannot be stored in 'DECIMAL' column anyway. – Evk Oct 24 '21 at 13:25
  • BigInteger doesn't exist in c# as datatype getting down it it an int32. But i don't understand why you want an Integer converted into a decimal – nbk Oct 24 '21 at 13:28
  • I edited the post to answer some of the questions. – moccajoghurt Oct 24 '21 at 13:35
  • 1
    `DECIMAL(36,0)` might make more sense – Charlieface Oct 24 '21 at 14:51
  • @Charlieface I agree, thank you. – moccajoghurt Oct 24 '21 at 18:14

1 Answers1

0

The solution is much simpler than expected. Calling ToString() does the job:

public void AddTrade(BigInteger price) {
    _connection.Open();
    var command = new MySqlCommand(@"INSERT INTO Prices (Price) VALUES (@Price);", _connection);
    command.Parameters.Add("@Price", MySqlDbType.Decimal).Value = price.ToString();
    command.ExecuteNonQuery();
    _connection.Close();
}
moccajoghurt
  • 159
  • 9