如何在加载数据后验证数据表行

时间:2017-01-04 17:34:11

标签: c# datatable console-application

我有一个从Informix表中读取并在其中存储值的数据表,如果zipcode列的值范围为3到9,那么我需要验证此列,然后将其发送到我的merge方法。

这是我的数据库代码

 public static DataTable GetGroup1Data()
        {
 string threeyearsago = (DateTime.Now.Year - 2).ToString();
OdbcConnection MyCon = new OdbcConnection();
 MyCon.ConnectionString = ConfigurationManager.ConnectionStrings["DBLive"].ConnectionString;
 MyCon.Open();
   OdbcCommand mycmd = new OdbcCommand();
   mycmd.Connection = MyCon;
   mycmd.CommandType = CommandType.Text;
 mycmd.CommandText = "{SET ISOLATION TO DIRTY READ} SELECT CASE WHEN LENGTH(REPLACE(zip,'-','')) <= '5' THEN RPad(RTRIM(zip),'9','0') WHEN LENGTH(REPLACE(zip,'-','')) > '5' AND LENGTH(REPLACE(zip,'-','')) < '9' THEN LPAD(LTRIM(zip),9,'0') ELSE Replace(zip,'-','') END AS ZIPCODE from sometable
DataSet custmaster = new DataSet();
 OdbcDataAdapter myadapter = new OdbcDataAdapter();
 myadapter.SelectCommand = mycmd;
 DataTable dt = new DataTable();
 dt.Load(mycmd.ExecuteReader());           
 mycmd.Dispose();
 MyCon.Close();
 return dt;
}

我现在要验证zipcode值的长度并做左边的垫或右边的垫,我该如何实现呢?

1 个答案:

答案 0 :(得分:0)

你可以试试这个:

foreach(DataRow d in dt.Rows)
{
     if(d["zip"].ToString().Length < 10) //example
     {
         //do something
     }
}

您可以使用PadLeft和PadRight进行填充。