SqlConnection conn = new SqlConnection();
conn.ConnectionString ="你的sql server数据库连接字符串";
conn.Open();
SqlTransaction sqlTransaction = conn.BeginTransaction();//定义事务
var sql = @"insert into Plant values(@OrganizationId,@Code,@Name,@ActiveFlag,@RegionId,@Location,@WashTime,@TimeUnitId,@OverheadCost,@OverheadUnitId)";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Transaction = sqlTransaction;//绑定事务
try
{
foreach (var n in data)
{ SqlParameter[] paras = new SqlParameter[]
{
new SqlParameter("@OrganizationId",n.OrganizationId),
new SqlParameter("@Code",n.Code),
new SqlParameter("@Name",n.Name),
new SqlParameter("@ActiveFlag",n.ActiveFlag),
new SqlParameter("@RegionId",n.RegionId),
new SqlParameter("@Location","POINT("+n.Long+" "+n.Lat+")"),////将点存到数据库中
new SqlParameter("@WashTime",n.WashTime),
new SqlParameter("@TimeUnitId",n.TimeUnitId),
new SqlParameter("@OverheadCost",n.OverheadCost),
new SqlParameter("@OverheadUnitId",n.OverheadUnitId)
};
cmd.Parameters.AddRange(paras);
cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
}
sqlTransaction.Commit();
}
catch (Exception e)
{
sqlTransaction.Rollback();
}
finally {
cmd.Dispose();
conn.Close();
conn.Dispose();
}
参考链接:https://www.cnblogs.com/wuyong/archive/2006/10/15/529514.html