本文介绍了[REPOST]如何将一个表(Asp表)中的所有数据复制到asp .net中的一个DataTable/DataSet中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友

我有一个表(asp表)中有数据.我想将所有这些数据复制到DataTable/Dataset中.如果有人对此有任何想法,请帮助我.



谢谢

Hi friends

I have a table(asp table) with data in it. I would like to copy all these data into a DataTable/Dataset. If anyone has idea about this please help me.



Thank You

推荐答案

foreach (TableRow tableRow in aspTable.Rows)
            {
                yourDataTable.Rows.Add(tableRow.Cell[0].Text, tableRow.Cell[1].Text,tableRow.Cell[2].Text, ...);
            }


using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void ShowData_Click(object sender, EventArgs e)
        {
            string connetionString = null;
            SqlConnection connection ;
            SqlCommand command ;
            SqlDataAdapter adapter = new SqlDataAdapter();
            DataSet ds = new DataSet();
            int i = 0;
            string Sql = null;

            connetionString = Your Connection string;
            Sql = "SQL Statement";

            connection = new SqlConnection(connetionString);

            try
            {
                connection.Open();

                command = new SqlCommand(Sql, connection);
                adapter.SelectCommand = command;
                adapter.Fill(ds, "First Table");

                adapter.Dispose();
                command.Dispose();
                connection.Close();

                //retrieve  data 
                for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
                {
                    MessageBox.Show(ds.Tables[0].Rows[i].ItemArray[0] + " -- " + ds.Tables[0].Rows[i].ItemArray[1]);
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show("Can not open connection ! ");
            }
        }
    }
}



这篇关于[REPOST]如何将一个表(Asp表)中的所有数据复制到asp .net中的一个DataTable/DataSet中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 21:13