学习笔记之DataTable
思维导图
知识点
创建DataTable数据表
创建一个表名为yp的数据表:
DataTable NewTable = new DataTable(yp);
创建DataTable对象的实例yp
DataTable workTable = new DataTable("yp");
添加DataTable列(Columns属性)
DataTable workTable = new DataTable("Customers");
DataColumn workCol = workTable.Columns.Add("CustID");
workTable.Columns.Add("CustLName");
workTable.Columns.Add("CustFName");
workTable.Columns.Add("Purchases");
添加DataTable行
DataTable CustomersTable = new DataTable("Customers ");
DataRow arow = CustomersTable.NewRow();
arow[ColumnName] = DataValue;
CustomersTable.Rows.Add(arow);
新建行的赋值
DataRow dr = dt.NewRow();
dr[0] = "张三";//通过索引赋值
dr["column1"] = DateTime.Now;
dt.Rows[0][0] = "张三"; //通过索引赋值
dt.Rows[0]["column1"] = DateTime.Now;
取值
string name=dt.Rows[0][0].ToString();
string time=dt.Rows[0]["column1"].ToString();
案例代码如下:
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.Data.SqlClient; namespace 药品工作站 { public partial class 追加入库 : Form { public 追加入库() { InitializeComponent(); this.StartPosition = FormStartPosition.CenterScreen; this.dgv.BackgroundColor = Color.White; this.dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells; }private void button2_Click(object sender, EventArgs e) { SqlConnection sqlConnection = new SqlConnection(); sqlConnection.ConnectionString = "Server=(local);Database=药品工作站管理系统;Integrated Security=sspi"; SqlCommand sqlCommand = new SqlCommand(); sqlCommand.Connection = sqlConnection; sqlCommand.CommandText = "SELECT * FROM 药品入库表;"; SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(); sqlDataAdapter.SelectCommand = sqlCommand; DataTable ypTable = new DataTable(); sqlConnection.Open(); sqlDataAdapter.Fill(ypTable); sqlConnection.Close(); this.dgv.DataSource = ypTable; }private void button3_Click_1(object sender, EventArgs e) { if (this.dgv.RowCount > 0) { DataRow currentypRow = ((DataRowView)this.dgv.CurrentRow.DataBoundItem).Row; no.Text =Convert.ToString(currentypRow["入库编号"]); ypno.Text = Convert.ToString(currentypRow["药品编号"]); number.Text = Convert.ToString(currentypRow["入库数量"]); time.Text = Convert.ToString(currentypRow["入库时间"]); man.Text = Convert.ToString(currentypRow["操作员"]); } } private void button1_Click(object sender, EventArgs e) { if (this.no.Text.Trim() == "") { MessageBox.Show("编号不能为空!"); this.no.Focus(); return; } if (this.ypno.Text.Trim() == "") { MessageBox.Show("药品编号不能为空!"); this.ypno.Focus(); return; } if (this.number.Text.Trim() == "") { MessageBox.Show("数量不能为空!"); this.number.Focus(); return; } if (this.time.Text.Trim() == "") { MessageBox.Show("时间不能为空!"); this.time.Focus(); return; } if (this.man.Text.Trim() == "") { MessageBox.Show("操作员不能为空!"); this.man.Focus(); return; } SqlConnection sqlConnection = new SqlConnection(); sqlConnection.ConnectionString = "Server=(local);Database=药品工作站管理系统;Integrated Security=sspi"; SqlCommand sqlCommand = new SqlCommand(); sqlCommand.Connection = sqlConnection; SqlCommand updateCommand = new SqlCommand(); updateCommand.Connection = sqlConnection; updateCommand.CommandText = "UPDATE 药品入库表" + " SET 入库编号=@入库编号,药品编号=@药品编号,入库数量=@入库数量,入库时间=@入库时间,操作员=@操作员" + " WHERE 采购编号=@Old采购编号;"; updateCommand.Parameters.Add("@入库编号", SqlDbType.Int, 0, this.no.Text.Trim()); updateCommand.Parameters.Add("@药品编号", SqlDbType.Int, 0, this.ypno.Text.Trim()); updateCommand.Parameters.Add("@入库数量", SqlDbType.Int, 0, this.number.Text.Trim()); updateCommand.Parameters.Add("@入库时间", SqlDbType.Int, 0, time.ToString()); updateCommand.Parameters.Add("@操作员", SqlDbType.Char, 0, this.man.Text.Trim()); SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(); sqlDataAdapter.UpdateCommand = updateCommand; DataTable Table1 = (DataTable)this.dgv.DataSource; sqlConnection.Open(); int rowAffected = sqlDataAdapter.Update(Table1); sqlConnection.Close(); MessageBox.Show("追加成功"); } } }
结果如下: