3

I have sql database table and doing select query i'm fetching data and want to insert that data into document db.

I tried like below -

 private const string EndpointUrl = "<your endpoint URL>";
        private const string PrimaryKey = "<your primary key>";
        private DocumentClient client;

        private async Task InsertIntoDocuemntDb()
        {
            this.client = new DocumentClient(new Uri(EndpointUrl), PrimaryKey);

            await this.client.CreateDatabaseIfNotExistsAsync(new Database { Id = "FamilyDB" });

            await this.client.CreateDocumentCollectionIfNotExistsAsync(UriFactory.CreateDatabaseUri("FamilyDB"), new DocumentCollection { Id = "FamilyCollection" });


        }

I can get sql data using below code -

using (SqlConnection connection = new SqlConnection(ConnectionString))
            {
                connection.Open();
                SqlCommand cmd = new SqlCommand("select name , rollId from demotable ", connection);
                cmd.CommandType = CommandType.Text;

                using (SqlDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())

how to insert list of string and insert into document db ?

Neo
  • 15,491
  • 59
  • 215
  • 405

1 Answers1

2

We can use Microsoft Azure Document API to achieve that.

We can insert data into cosmos db(Document) as below:

public static async void InsertDoc(DocumentClient client, string collectionLink, Document doc)
    {
        Document created = await client.CreateDocumentAsync(collectionLink, doc);
    }

In your code, we can do as below:

        using (SqlConnection connection = new SqlConnection(ConnectionString))
        {
            connection.Open();
            SqlCommand cmd = new SqlCommand("select name , rollId from demotable ", connection);
            cmd.CommandType = CommandType.Text;

            using (SqlDataReader reader = cmd.ExecuteReader())
            {
                while (reader.Read()){
                   dynamic document = new Document();
                   document.name = reader["name"].ToString(); 
                   document.rollId = reader["rollId"].ToString();
                   InsertDoc(client, UriFactory.CreateDocumentCollectionUri("FamilyDB", "FamilyCollection"), document);
                }
            }

        }

More information about Azure Document API, we can refer to: Document API

Lee Liu
  • 1,981
  • 1
  • 12
  • 13
  • what i did is i took list and pass it to CreateDocumentAsync getting error- Object serialized to Array. JObject instance expected , any soltuion ? – Neo Jun 26 '18 at 10:41
  • 1
    I do not want to go one by one i want one document with list of items – Neo Jun 26 '18 at 10:58
  • To insert multiple data, we can use bulk executor library: https://github.com/Azure/azure-cosmosdb-bulkexecutor-dotnet-getting-started – Lee Liu Jun 27 '18 at 02:24
  • We can also refer to this thread: https://stackoverflow.com/questions/36063169/how-to-upload-multiple-document-bulk-in-document-db – Lee Liu Jun 27 '18 at 02:38