.net 从Cosmos数据库向Sql数据库插入数据
我正在寻求帮助,我正在尝试使用.Net将数据从Cosmos db移动到Sql db。所以,在这个过程中,我在这里面临着一个问题 此cnnisert.Close(),在每次插入记录后关闭,如何避免每次插入记录后关闭.net 从Cosmos数据库向Sql数据库插入数据,.net,azure-cosmosdb,sqldb,.net,Azure Cosmosdb,Sqldb,我正在寻求帮助,我正在尝试使用.Net将数据从Cosmos db移动到Sql db。所以,在这个过程中,我在这里面临着一个问题 此cnnisert.Close(),在每次插入记录后关闭,如何避免每次插入记录后关闭 enter code here commInsert.Parameters.AddWithValue("@xxxx", "xxxx"); commInsert.Parameters.AddWith
enter code here
commInsert.Parameters.AddWithValue("@xxxx", "xxxx");
commInsert.Parameters.AddWithValue("xxxxx", "tobedeleted");
commInsert.Parameters.AddWithValue("xxxxx", xxxxxx);
commInsert.Parameters.AddWithValue("xxxx", "tobedeleted");
commInsert.Parameters.AddWithValue("xxxx", xxxxx);
commInsert.ExecuteNonQuery();
flag = true;
cnnInsert.Close();
Console.WriteLine("records updated for " + email);
}
简单地说,不要写这行代码:
cnnisert.Close()
但是,我坚信,应该处理实现IDisposable
接口的每个对象。您可以使用以下内容作为最佳实践:
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
using (SqlCommand command1 = new SqlCommand(query1, connection))
{
// Execute command, etc. here
}
using (SqlCommand command2 = new SqlCommand(query2, connection))
{
// Execute command, etc. here
}
using (SqlCommand command3 = new SqlCommand(query3, connection))
{
// Execute command, etc. here
}
}
感谢您的回答,但由于不使用这一行,cnnisert.Close()代码根本不起作用,并且感谢您对对象进行去定义的新实践,我将尝试在以下方面排除此问题: