C# 如何从excel导入documentDB中的批量数据?
我已经浏览了一天关于如何从excel文件在documentDB中插入批量数据,但没有得到任何信息 我可以从excel文件中读取数据并在documentDB中逐个插入C# 如何从excel导入documentDB中的批量数据?,c#,excel,insert,bulkinsert,azure-cosmosdb,C#,Excel,Insert,Bulkinsert,Azure Cosmosdb,我已经浏览了一天关于如何从excel文件在documentDB中插入批量数据,但没有得到任何信息 我可以从excel文件中读取数据并在documentDB中逐个插入 Service service = new Service(); foreach(data in exceldata) //exceldata contains set of rows { var student = new Student(); student.id= ""; student
Service service = new Service();
foreach(data in exceldata) //exceldata contains set of rows
{
var student = new Student();
student.id= "";
student.name = data.name;
student.age = data.age;
student.class = data.class;
student.id = service.savetoDocumentDB(collectionLink,student); //collectionlink is a string stored in web.config
students.add(student);
}
Class Service
{
public async Task<string> AddDocument(string collectionLink, Student data)
{
this.DeserializePayload(data);
var result = await Client.CreateDocumentAsync(collectionLink, data);
return result.Resource.Id;
}
}
服务=新服务();
foreach(exceldata中的数据)//exceldata包含一组行
{
var student=新学生();
学生编号=”;
student.name=data.name;
student.age=data.age;
student.class=data.class;
student.id=service.savetoDocumentDB(collectionLink,student);//collectionLink是存储在web.config中的字符串
学生。添加(学生);
}
班级服务
{
公共异步任务AddDocument(字符串集合链接,学生数据)
{
这是反序列化有效载荷(数据);
var result=await Client.CreateDocumentAsync(collectionLink,数据);
返回result.Resource.Id;
}
}
我可以在一个实例中插入所有记录吗
任何帮助都将非常可观。更新(4/8/15):DocumentDB刚刚发布了一个数据导入工具,它支持JSON文件、MongoDB、SQL Server和CSV文件。你可以在这里找到它:
原始答案:
DocumentDB还不支持从excel文件大容量导入。。。但是,您可以利用DocumentDB的存储过程使批量导入更友好一些
退房:
您可以在此处找到有关DocumentDB数据库端编程(存储过程、触发器和UDF)的参考文档:上面的链接已断开。应该是:
/**
* This script called as stored procedure to import lots of documents in one batch.
* The script sets response body to the number of docs imported and is called multiple times
* by the client until total number of docs desired by the client is imported.
* @param {Object[]} docs - Array of documents to import.
*/
function bulkImport(docs) {
var collection = getContext().getCollection();
var collectionLink = collection.getSelfLink();
// The count of imported docs, also used as current doc index.
var count = 0;
// Validate input.
if (!docs) throw new Error("The array is undefined or null.");
var docsLength = docs.length;
if (docsLength == 0) {
getContext().getResponse().setBody(0);
}
// Call the CRUD API to create a document.
tryCreate(docs[count], callback);
// Note that there are 2 exit conditions:
// 1) The createDocument request was not accepted.
// In this case the callback will not be called, we just call setBody and we are done.
// 2) The callback was called docs.length times.
// In this case all documents were created and we don't need to call tryCreate anymore. Just call setBody and we are done.
function tryCreate(doc, callback) {
var isAccepted = collection.createDocument(collectionLink, doc, callback);
// If the request was accepted, callback will be called.
// Otherwise report current count back to the client,
// which will call the script again with remaining set of docs.
// This condition will happen when this stored procedure has been running too long
// and is about to get cancelled by the server. This will allow the calling client
// to resume this batch from the point we got to before isAccepted was set to false
if (!isAccepted) getContext().getResponse().setBody(count);
}
// This is called when collection.createDocument is done and the document has been persisted.
function callback(err, doc, options) {
if (err) throw err;
// One more document has been inserted, increment the count.
count++;
if (count >= docsLength) {
// If we have created all documents, we are done. Just set the response.
getContext().getResponse().setBody(count);
} else {
// Create next document.
tryCreate(docs[count], callback);
}
}
}