Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 从Cosmos数据库向Sql数据库插入数据_.net_Azure Cosmosdb_Sqldb - Fatal编程技术网

.net 从Cosmos数据库向Sql数据库插入数据

.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

我正在寻求帮助,我正在尝试使用.Net将数据从Cosmos db移动到Sql db。所以,在这个过程中,我在这里面临着一个问题

此cnnisert.Close(),在每次插入记录后关闭,如何避免每次插入记录后关闭

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()代码根本不起作用,并且感谢您对对象进行去定义的新实践,我将尝试在以下方面排除此问题: