如何简化此冗长的ADO.NET数据库代码

时间:2019-01-22 12:21:27

标签: c# ado.net refactoring

有疑问的人-我对C#不熟悉。

我有以下代码来调用多参数SQL-Server存储过程。 它可以正常工作,效果很好,也很明确,即易于阅读正在发生的事情,但是非常冗长。

以下是标准的简短版本吗?

using(var conn = new SqlConnection(this.connString))
using(var comm = new SqlCommand(DatabaseLayer.procSendEmail,conn))
{
  comm.CommandType = CommandType.StoredProcedure;

  SqlParameter pmIsTemplate = new SqlParameter("@IsTemplate",SqlDbType.TinyInt);
  SqlParameter pmDateInsertKey = new SqlParameter("@DateInsertKey",SqlDbType.Int);
  SqlParameter pmEmailTO = new SqlParameter("@EmailTO",SqlDbType.NVarChar,1000);
  SqlParameter pmEmailBody = new SqlParameter("@EmailBody",SqlDbType.NVarChar,-1);
  SqlParameter pmEmailImportance = new SqlParameter("@EmailImportance",SqlDbType.TinyInt);
  SqlParameter pmEmailSubject = new SqlParameter("@EmailSubject",SqlDbType.NVarChar,1000);
  SqlParameter pmSuccess = new SqlParameter("@Success",SqlDbType.Bit);

  pmIsTemplate.Direction        = ParameterDirection.Input;
  pmDateInsertKey.Direction     = ParameterDirection.Input;
  pmEmailTO.Direction           = ParameterDirection.Input;
  pmEmailBody.Direction         = ParameterDirection.Input;
  pmEmailImportance.Direction   = ParameterDirection.Input;
  pmEmailSubject.Direction      = ParameterDirection.Input;
  pmSuccess.Direction           = ParameterDirection.Output;

  comm.Parameters.Add(pmIsTemplate);
  comm.Parameters.Add(pmDateInsertKey);
  comm.Parameters.Add(pmEmailTO);
  comm.Parameters.Add(pmEmailBody);
  comm.Parameters.Add(pmEmailImportance);
  comm.Parameters.Add(pmEmailSubject);
  comm.Parameters.Add(pmSuccess);

  ...
  ...

1 个答案:

答案 0 :(得分:1)

也许正在做这样的事情:

library(ncdf4)
library(raster)
temp <- nc_open("dataset-ibi-reanalysis-phys-005-002-monthly_1547718004288.nc",write=TRUE)


TEMP = ncvar_get(temp,"bottomT")

latitude = ncvar_get(temp,"latitude")
longitude = ncvar_get(temp,"longitude")
nc_close(temp)