我需要使用表创建一个新数据集,并使用Microsoft.PowerBI.Api框架向该表添加行。我正在寻找一些关于如何完成的c#代码示例。
var tokenCredentials = new TokenCredentials(_authenticationResult.AccessToken, "Bearer");
using (var client = new PowerBIClient(new Uri(ApiUrl), tokenCredentials))
{
????
}
答案 0 :(得分:0)
您可以使用2种方法,如:
中所述https://docs.microsoft.com/en-us/power-bi/developer/walkthrough-push-data-add-rows
https://msdn.microsoft.com/library/mt203561.aspx
Microsoft.PowerBI.API
:using Microsoft.PowerBI.Api.V2;
...
var tokenCredentials = new TokenCredentials(_authenticationResult.AccessToken, "Bearer");
using (var client = new PowerBIClient(new Uri(ApiUrl), tokenCredentials))
{
string data = @"{ ""rows"":
[
{ ""id"": 1, ""name"": ""Tom""},
{ ""id"": 2, ""name"": ""Jerry""}
]
}";
var dataJson = Newtonsoft.Json.JsonConvert.DeserializeObject<Object>(data.ToString());
client.Datasets.PostRowsInGroup(groupId, datasetId, tableName, datajson);
}
private static void AddRows(string datasetId, string tableName)
{
string powerBIApiAddRowsUrl = String.Format("https://api.powerbi.com/v1.0/myorg/datasets/{0}/tables/{1}/rows", datasetId, tableName);
//POST web request to add rows.
//To add rows to a dataset in a group, use the Groups uri: https://api.powerbi.com/v1.0/myorg/groups/{group_id}/datasets/{dataset_id}/tables/{table_name}/rows
//Change request method to "POST"
HttpWebRequest request = System.Net.WebRequest.Create(powerBIApiAddRowsUrl) as System.Net.HttpWebRequest;
request.KeepAlive = true;
request.Method = "POST";
request.ContentLength = 0;
request.ContentType = "application/json";
//Add token to the request header
request.Headers.Add("Authorization", String.Format("Bearer {0}", token));
//JSON content for product row
string rowsJson = "{\"rows\":" +
"[{\"ProductID\":1,\"Name\":\"Adjustable Race\",\"Category\":\"Components\",\"IsCompete\":true,\"ManufacturedOn\":\"07/30/2014\"}," +
"{\"ProductID\":2,\"Name\":\"LL Crankarm\",\"Category\":\"Components\",\"IsCompete\":true,\"ManufacturedOn\":\"07/30/2014\"}," +
"{\"ProductID\":3,\"Name\":\"HL Mountain Frame - Silver\",\"Category\":\"Bikes\",\"IsCompete\":true,\"ManufacturedOn\":\"07/30/2014\"}]}";
//POST web request
byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(rowsJson);
request.ContentLength = byteArray.Length;
//Write JSON byte[] into a Stream
using (Stream writer = request.GetRequestStream())
{
writer.Write(byteArray, 0, byteArray.Length);
var response = (HttpWebResponse)request.GetResponse();
Console.WriteLine("Rows Added");
Console.ReadLine();
}
}