如何从另一个数据表更新dataTable的某些列数据 [英] How to Update certain column data of dataTable from another datatable

查看:297
本文介绍了如何从另一个数据表更新dataTable的某些列数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个DataTables

1. DTForQueNewEntry

2. dtFinal



我需要写一个执行下面的查询工作的代码



I have 2 DataTables
1. DTForQueNewEntry
2. dtFinal

I need to write a code which does the work like following query

update DTForQueNewEntry SET DTForQueNewEntry.OptionSet = dtFinal.OptionSet

where DTForQueNewEntry.Point_Mapping_Sr= dtFinal.Point_Mapping_Sr AND
    DTForQueNewEntry.Question_Name = dtFinal.Question_Name





where OptionSet Point_Mapping_Sr Question_Name 是两个DataTables中的列



感谢高级



where OptionSet, Point_Mapping_Sr, Question_Name are Columns present in both DataTables

Thanks in Advance

推荐答案

有两种可能性: -



1.为您的查询创建一个SP提到并在你的代码中调用它。



There are two possibility :-

1. Create a SP for the query you have mentioned and call it in your code.

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Databasename"].ConnectionString); {
    using (SqlCommand cmd = new SqlCommand("sp_Name;, con)) {
      cmd.CommandType = CommandType.StoredProcedure;

      con.Open();
      cmd.ExecuteNonQuery();







2.将查询称为它在你的代码中。






2. Call the query as it is in your code.

 SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Databasename"].ConnectionString);
 SqlCommand cmd1 = new SqlCommand("update DTForQueNewEntry SET DTForQueNewEntry.OptionSet = dtFinal.OptionSet 
where DTForQueNewEntry.Point_Mapping_Sr= dtFinal.Point_Mapping_Sr AND
    DTForQueNewEntry.Question_Name = dtFinal.Question_Name");
 con.Open();
cmd1.ExecuteNonQuery();
con.Close();





- NP



- NP


我做了以下这解决了这个问题。谢谢



I did the following which solved the issue. Thanks

DataRow[] drs = dtWithScore.Select("OptionSet = 'true'");
       DataTable dtFinal = dtWithScore.Clone();
       foreach (DataRow d in drs)
       {
           dtFinal.ImportRow(d);
       }
       for (int i = 0; i < dtFinal.Rows.Count; i++)
       {
           Int64 intPoint_Mapping_Sr = Convert.ToInt64(dtFinal.Rows[i]["Point_Mapping_Sr"]);
           string strQuestionOptions_OptionName = dtFinal.Rows[i]["QuestionOptions_OptionName"].ToString();
           DataRow[] drToupdate = DTForQueNewEntry.Select("Point_Mapping_Sr = " + intPoint_Mapping_Sr + " AND QuestionOptions_OptionName = '" + strQuestionOptions_OptionName + "'");

           drToupdate[0]["OptionSet"] = true;

       }


这篇关于如何从另一个数据表更新dataTable的某些列数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆