ADO.Net中DataTable的应用

ADO.Net中DataTable的应用

一、思维导图

 

二、相关知识点

DataTable内存中的数据表,而非数据库的表。数据库中存储的是实体表,实体表中有一系列的数据。而DataTable即存储在内存中的表,是可以独立创建和使用的。每个表包含数据行DataRow和列DataColumn,所有的行构成数据行集DataRowCollection,所有的列构成数据列集DataColumnCollection。

三、效果截图和示例代码

演示:

 

代码:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.Configuration;

using System.Data.SqlClient;  

 

namespace 试验

{

    public partial class Form1 : Form

    {

        private DataTable PatientTable;

        private DataView  PatientViewByName;

        private DataView  DoctorViewByNo;

 

        public Form1()

        {

            InitializeComponent();

            this.StartPosition = FormStartPosition.CenterScreen;

            this.dataGridView1.AllowUserToAddRows = false;

            this.dataGridView1.RowHeadersVisible = false;

            this.dataGridView1.BackgroundColor = Color.White;

            this.dataGridView1.AutoSizeColumnsMode =

                DataGridViewAutoSizeColumnsMode.AllCells;

        }

 

        private void button1_Click(object sender, EventArgs e)

        {

            SqlConnection sqlConnection = new SqlConnection();

            sqlConnection.ConnectionString =

                "Server=(local);Database=XX;Integrated Security=sspi";

            SqlCommand sqlCommand = new SqlCommand();

            sqlCommand.Connection = sqlConnection;

            sqlCommand.CommandText = "SELECT * FROM tb_Patient;";

            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();

            sqlDataAdapter.SelectCommand = sqlCommand;

            sqlDataAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;

            this.PatientTable = new DataTable();

            sqlConnection.Open();

            sqlDataAdapter.Fill(this.PatientTable);

            sqlConnection.Close();

            this.PatientViewByName = new DataView();

            this.PatientViewByName.Table = this.PatientTable;

            this.PatientViewByName.Sort = "PatientNo ASC";

            this.dataGridView1.DataSource = this.PatientTable;

            this.dataGridView1.Columns["PatientNo"].HeaderText = "病人编号";

            this.dataGridView1.Columns["PatientName"].HeaderText = "病人姓名";

            this.dataGridView1.Columns["Department"].HeaderText = "科室";

            this.dataGridView1.Columns["DoctorNo"].HeaderText = "医生编号";

            this.dataGridView1.Columns["DoctorName"].HeaderText = "医生姓名";

            this.dataGridView1.Columns[this.dataGridView1.Columns.Count - 1].AutoSizeMode =

                DataGridViewAutoSizeColumnMode.Fill;

 

        }

 

        private void button2_Click(object sender, EventArgs e)

        {

            DataRow searchResultRow = this.PatientTable.Rows.Find(this.textBox1.Text.Trim());

            DataTable searchResultTable = this.PatientTable.Clone();

            searchResultTable.ImportRow(searchResultRow);

            this.dataGridView1.DataSource = searchResultTable;

        }

    }

}

 

 

posted on 2018-10-31 19:26  fenglianchen  阅读(161)  评论(0编辑  收藏  举报

导航