DataTable是一个临时保存数据的网格虚拟表(表示内存中数据的一个表。)。DataTable是ADO dot net 库中的核心对象。它可以被应用在 VB 和 ASP 上。它无须代码就可以简单的绑定数据库。它具有微软风格的用户界面。
用法介绍:
(1)可以使用相应的 DataTable 构造函数创建 DataTable 对象。 可以通过使用 Add 方法将其添加到 DataTable 对象的 Tables集合中,将其添加到 DataSet 中。
(2)使用 DataAdapter 对象的 Fill 方法或 FillSchema 方法在 DataSet 中创建,或者使用 DataSet 的 ReadXml、ReadXmlSchema 或InferXmlSchema 方法从预定义的或推断的 XML 架构中创建。 请注意,将一个 DataTable 作为成员添加到一个 DataSet 的 Tables 集合中后,不能再将其添加到任何其他 DataSet 的表集合中。
(3)在为 DataTable 定义了架构之后,可通过将 DataRow 对象添加到表的 Rows 集合中来将数据行添加到表中。
处理数据:
(1)向数据表中添加数据
(2)说明如何创建新行并将它们添加到表中。
(3)查看数据表中的数据
(4)说明如何访问行中的数据,包括数据的原始版本和当前版本。
(5)Load 方法
(6)说明如何通过 Load 方法使用行填充 DataTable。
(7)DataTable 编辑
(8)说明如何修改行中的数据,包括挂起对行的更改,直至验证并接受了建议的更改。
(9)行状态与行版本
(10)提供有关行的不同状态的信息。
(11)DataRow 删除
(12)说明如何从表中移除行。
(13)行错误信息
(14)说明如何插入每行的错误信息,帮助解决应用程序中的数据问题。
(15)AcceptChanges 和 RejectChanges
(16)说明如何接受或拒绝对行的更改。
初始化 DataTable 类的新实例:
重载此成员。有关此成员的完整信息(包括语法、用法和示例),请单击重载列表中的相应名称。
名称 | 说明 |
DataTable() | 不带参数初始化 DataTable 类的新实例。 |
DataTable(String) | 用指定的表名初始化 DataTable 类的新实例。 |
DataTable(SerializationInfo, StreamingContext) | 使用 SerializationInfo 和 StreamingContext 初始化 DataTable 类的新实例。 |
DataTable(String,String) | 用指定的表名和命名空间初始化 DataTable 类的新实例。 |
属性
名称 | 属性 |
CaseSensitive | 指示表中的字符串比较是否区分大小写。 |
ChildRelations | 获取此 DataTable 的子关系的集合。 |
Columns | 获取属于该表的列的集合。 |
Constraints | 获取由该表维护的约束的集合。 |
Container | 获取组件的容器。 (继承自 MarshalByValueComponent。) |
DataSet | 获取此表所属的 DataSet。 |
DefaultView | 获取可能包括筛选视图或游标位置的表的自定义视图。 |
DesignMode | 获取指示组件当前是否处于设计模式的值。 (继承自 MarshalByValueComponent。) |
DisplayExpression | 获取或设置一个表达式,该表达式返回的值用于表示用户界面中的此表。 DisplayExpression 属性用于在用户界面中显示此表的名称。 |
Events | 获取附加到该组件的事件处理程序的列表。 (继承自 MarshalByValueComponent。) |
ExtendedProperties | 获取自定义用户信息的集合。 |
代码实例:
创建表:
private DataTable PillTable;
private DataTable PrePillTable;
private DataView PillViewByName;
this.PillTable = new DataTable();
DataTable searchResultTable = this.PillTable.Clone();
创建列:
DataColumn dc=new DataColumn();
dt.Columns.Add(dc);
dt.Columns.Add("总价",typeof(String));
创建行:
private void txb_Pinyin_TextChanged_1(object sender, EventArgs e)
{
DataRow[] searchResultRows =
this.PillTable.Select("pinyin LIKE '%" + this.txb_Pinyin.Text.Trim() + "%'");
DataTable searchResultTable = this.PillTable.Clone();
foreach (DataRow row in searchResultRows)
{
searchResultTable.ImportRow(row);
}
this.dgv_SearchMedicine.DataSource = searchResultTable;
}
复制表:
this.PillTable = new DataTable();
sqlConnection.Open();
sqlDataAdapter.Fill(this.PillTable);
sqlConnection.Close();
this.PrePillTable = this.PillTable.Copy();
this.PillViewByName = new DataView();
this.PillViewByName.Table = this.PillTable;
this.PillViewByName.Sort = "realmediname ASC";
this.dgv_SearchMedicine.Columns.Clear();
this.dgv_SearchMedicine.DataSource = this.PillTable;
具体实例:
private void btn_Load_Click(object sender, EventArgs e)
{
SqlConnection sqlConnection = new SqlConnection();
sqlConnection.ConnectionString =
"Server=(local);Database=MEDI;Integrated Security=sspi";
SqlCommand sqlCommand = new SqlCommand();
sqlCommand.Connection = sqlConnection;
sqlCommand.CommandText = "SELECT * FROM tb_AddMedicine;";
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();
sqlDataAdapter.SelectCommand = sqlCommand;
sqlDataAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
this.PillTable = new DataTable();
sqlConnection.Open();
sqlDataAdapter.Fill(this.PillTable);
sqlConnection.Close();
this.PrePillTable = this.PillTable.Copy();
this.PillViewByName = new DataView();
this.PillViewByName.Table = this.PillTable;
this.PillViewByName.Sort = "realmediname ASC";
this.dgv_SearchMedicine.Columns.Clear();
this.dgv_SearchMedicine.DataSource = this.PillTable;
this.dgv_SearchMedicine.Columns["medicineno"].HeaderText = "编号";
this.dgv_SearchMedicine.Columns["shangno"].HeaderText = "商品编号";
this.dgv_SearchMedicine.Columns["realmediname"].HeaderText = "药品名";
this.dgv_SearchMedicine.Columns["jinji"].HeaderText = "禁忌症";
this.dgv_SearchMedicine.Columns["shiyong"].HeaderText = "适用人群";
this.dgv_SearchMedicine.Columns["price"].HeaderText = "价格";
this.dgv_SearchMedicine.Columns["zhiyao"].HeaderText = "制药编号";
this.dgv_SearchMedicine.Columns["shengchan"].HeaderText = "生产日期";
this.dgv_SearchMedicine.Columns["baozhi"].HeaderText = "保质期";
this.dgv_SearchMedicine.Columns["jiliang"].HeaderText = "用药剂量";
this.dgv_SearchMedicine.Columns["zhiyaono"].HeaderText = "制药编号";
this.dgv_SearchMedicine.Columns["oldno"].Visible = false;
this.dgv_SearchMedicine.Columns["pinyin"].Visible = false;
this.dgv_SearchMedicine.Columns["tongyong"].Visible = false;
this.dgv_SearchMedicine.Columns["shangpin"].Visible = false;
this.dgv_SearchMedicine.Columns["leibie"].Visible = false;
this.dgv_SearchMedicine.Columns["leixing"].Visible = false;
this.dgv_SearchMedicine.Columns[this.dgv_SearchMedicine.Columns.Count - 1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
}
运行结果:
1.载入
2.按照编号搜索
3.按照名称搜索
4.拼音搜索