zoukankan      html  css  js  c++  java
  • DataTable中动态的赋值 转

    DataTable中动态的赋值
    2009-07-04 20:50
    DataTable中动态的赋值

        // Process Activity Data
        DataTable dtSession = new DataTable("sesson");//新建一个名为session的table
       
        DataRow drow;
        DataColumn dc;
       
        // transpose the table
        drow = dtSession.NewRow();//session表中增加一行

        foreach (DataRow row in ActivityData.Tables["activity_data"].Rows) //ActivityData 为dataset,activity_data是个表名这个foreach中是把ActivityData中Activity_data的列名考到 session表中
        {
         dc = new DataColumn();//行中增加一个单元 格
         dc.DataType = System.Type.GetType("System.String");//定义单元格的属性是 string,还是其他类型(从Excel中把数据导入到sql中字符默认的是varchar,数字默认的 是double?这个不太确定(得要确认!!))
         dc.ColumnName = row["Row_IDName"].ToString();//把activity_data中的列明赋值给新增加的单元格
         //add by claire (这个地方是我增加的,以为pdf 中显示的model_desc在前台显示总是错的,所以在取的时候先不把Model_desc放进去了,到后面再处理)
         if(dc.ColumnName=="Model_Desc"){
          continue;
         }
         //end
         dc.ReadOnly = false;
         dtSession.Columns.Add(dc);
         drow[row["Row_IDName"].ToString()] = row["Data"].ToString();//把activity_data表中的值再付给新增加的 单元格
        }

       dtSession.Rows.Add(drow);

       string strYear = dtSession.Rows[0]["Car Year"].ToString();//从现有的session中取值

       string strModel = dtSession.Rows[0]["_Model"].ToString();

        basicDBFunction oDBAccess = new basicDBFunction(System.Configuration.ConfigurationSettings.AppSettings["RegistryString"]);
        
         SqlCommand oCommand = new SqlCommand();
         DataSet dsData = new DataSet(); //新生成一个dataset用来存放sc_get_mb_model的查询出来的值

    //add by claire for damn pdf
         // handle Model_Desc here
         try
         {
        
          oCommand.Connection = oDBAccess.dbConnection;
          oCommand.CommandType = CommandType.StoredProcedure;//注意存储过程的类型一定要注明,如果不注明的话可能后面的dataset会得到不到值。(记得处理Kelvin 的用sqlserver profile能打印存储过程,不过就是再后面得不到dataset的值)
          oCommand.CommandTimeout = 120;
          oCommand.CommandText = "sc_get_mb_model";//得model_desc的存储过程

          oCommand.Parameters.Clear();
          oCommand.Parameters.Add("@Model", strModel);//给存储过程赋参数
          oCommand.Parameters.Add("@Year", strYear);

          dsData.Tables.Add("Model");//把增加的dataset增加Model table
          oDBAccess.SelectFromDB(dsData, "Model", oCommand);
         }
         catch (Exception ex)
         {
         
         }
         finally
         {
          oDBAccess.closeConnection();//在 处理数据库的时候要加try---catch
         }
         // Add Model_desc
         try{
          dc = new DataColumn(); //增加一个column
          dc.DataType = System.Type.GetType("System.String");
          dc.ColumnName = "Model_Desc";
          dc.ReadOnly = false;
          dtSession.Columns.Add(dc);
          dtSession.Rows[0]["Model_Desc"] = dsData.Tables["Model"].Rows[0]["Model"].ToString();
         }// for model that can't be found
         catch(Exception ex)
         {
          System.Diagnostics.Debug.Write(ex.Message); //在处理web工程的时候用System.Console.Write(ex.Message)是没法打印值的,要看 打印的值窗口 View-->Other Windows-->Output
         }
         //end

  • 相关阅读:
    Vue之常用语法
    Django之crm
    Django项目之客户
    前端、数据库、Django简单的练习
    Django的认证系统
    Django之form表单
    OpneCV 二值图像区域处理
    OpenCV图像处理中常用函数汇总(1)
    OpenCV3编程入门笔记(6)自我验证各函数程序代码
    OpenCV_轮廓的查找、表达、绘制、特性及匹配
  • 原文地址:https://www.cnblogs.com/liye/p/1738550.html
Copyright © 2011-2022 走看看