Home > Blockchain >  How to pass query to fetch data from dynamo db
How to pass query to fetch data from dynamo db

Time:07-22

I used to work with java and SQL where we can have SQL.properties kind file where we can mention a query and data is returned

query = "select * from tablename"

But in .net how can we write a query and fetch data instead of methods provided by dynamodb like getitemasync() or async()

CodePudding user response:

Writing static queries as string sounds like problem when you have to insert many dynamic values.

Can you use the library specifically provided for using DynamoDB under .net ?

https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/ItemCRUDDotNetDocumentAPI.html

CodePudding user response:

Well first of all, let me recommend you do not do that, but instead investigate entity framework, you really have to start using code first automatic binding.

But for legacy point you can use a package System.Data.SqlClient for super duper manual handling of every f'ing thing ... jup done it for ages, once.

public class OldDb
{
    public void OldDbHandling()
    {
        var sql = "select * from acme";
        try
        {
            using var cn = new System.Data.SqlClient.SqlConnection("secretstuff");
            cn.Open();
            if(cn.State != System.Data.ConnectionState.Open)
            {
                //TODO: Complain about this
            }
            using var cmd = new System.Data.SqlClient.SqlCommand(sql, cn);
            var reader = cmd.ExecuteReaderAsync().GetAwaiter().GetResult();
           while (reader.Read())
            {
                var variable = (typeincolumn)reader["MyColumnName"];
            }
           reader.Close();
        } catch (Exception ex)
        {
            //TODO: Handle or ...
            throw;
        }
    }
}

So but if can choose don't do this, instead investigte entity framework, you don't need to work hard when you can work smart: https://docs.microsoft.com/en-us/ef/core/get-started/overview/first-app?tabs=netcore-cli You will really spend months less for the investment of weeks if you cross this chasm

  • Related