ADO.Net中Data Table的应用

ADO.Net中Data Table的应用

创建Data table并载入到DataGridView

SqlConnection sqlConnection = new SqlConnection();                                              
sqlConnection.ConnectionString ="Server=DESKTOP-KSC5TFT\\SQLEXPRESS;Database=OperationManagement;Integrated Security=sspi";
SqlCommand sqlCommand2 = new SqlCommand();                                                       
sqlCommand2.Connection = sqlConnection;
sqlCommand2.CommandText = "select * from operating ;";                                            
SqlDataAdapter sqlDataAdapter2 = new SqlDataAdapter();                                           
sqlDataAdapter2.SelectCommand = sqlCommand2;                                                      
sqlDataAdapter2.MissingSchemaAction = MissingSchemaAction.AddWithKey;                            
this.opname = new DataTable();
sqlConnection.Open();
sqlDataAdapter2.Fill(this.opname);
sqlConnection.Close();
this.dgv_opname.DataSource = this.opname;
this.dgv_opname.Columns["OPNo"].HeaderText = "编号";
this.dgv_opname.Columns["OPName"].HeaderText = "手术名称";
this.dgv_opname.Columns["OPcost"].HeaderText = "手术费用";
this.dgv_opname.Columns["OPtype"].HeaderText = "手术类型";
this.dgv_opname.Columns["pinyin"].Visible = false;

效果截图:

ADO.Net中Data Table的应用