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 ?
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.