小编典典

将SQL表读入C#DataTable

c#

我读过很多关于将DataTable插入SQL表的文章,但是有没有一种简单的方法可以将SQL表插入.NET DataTable?


阅读 366

收藏
2020-05-19

共1个答案

小编典典

在这里,给它一个镜头(这只是一个伪代码)

using System;
using System.Data;
using System.Data.SqlClient;


public class PullDataTest
{
    // your data table
    private DataTable dataTable = new DataTable();

    public PullDataTest()
    {
    }

    // your method to pull data from database to datatable   
    public void PullData()
    {
        string connString = @"your connection string here";
        string query = "select * from table";

        SqlConnection conn = new SqlConnection(connString);        
        SqlCommand cmd = new SqlCommand(query, conn);
        conn.Open();

        // create data adapter
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        // this will query your database and return the result to your datatable
        da.Fill(dataTable);
        conn.Close();
        da.Dispose();
    }
}
2020-05-19