时间:2022-08-03 12:21:31 | 栏目:.NET代码 | 点击:次
DataSet ds = new DataSet(); SqlCommand cmd = new SqlCommand("select * from Catogories;select * from Customers", conn); SqlDataAdapter da = new SqlDataAdapter(cmd);//da可以多次使用于不同的SelectCommand.CommandText DataTableMapping map = da.TableMappings.Add("Table", "Customer");//参数依次为,数据源中表,DataSet中的表 map.ColumnMappings.Add("Name", "CustomerName"); //da.MissingMappingAction= MissingMappingAction.Ignore; da.MissingSchemaAction = MissingSchemaAction.AddWithKey;//用于处理模式冲突,默认为Add。 da.Fill(ds);//新增的DataTable表名默认为"Table",而后新增的表名默认为Table1,Table2等。 da.Fill(ds, "Customer"); da.Fill(ds, 0, 10000, "Customer");//用于分页填充 da.FillSchema(ds, System.Data.SchemaType.Source, "Customer");//仅仅填充模式信息
如果连接未打开,则会自动打开,Fill后自动关闭,如还需要使用此连接,需要重新打开。如果conn手工已打开,Fill后需要手动关闭连接。
SqlCommand cmd = new SqlCommand("select * from Catogories;s", conn); SqlDataAdapter da = new SqlDataAdapter(cmd); da.Fill(ds, "Catogories"); cmd.CommandText = "select * from Customers"; SqlDataAdapter da2 = new SqlDataAdapter(cmd); da2.Fill(ds, "Customer");
SqlCommand cmd = new SqlCommand("select * from Catogories;s", conn); SqlDataAdapter da = new SqlDataAdapter(cmd); da.Fill(ds, "Catogories") da.SelectCommand.CommandText = "select * from Customers"; da.Fill(ds, "Customer");
SqlCommand cmd = new SqlCommand("select * from Catogories;select * from Customers", conn); SqlDataAdapter da = new SqlDataAdapter(cmd); da.Fill(ds);
DataSet dsChanged = ds.GetChanges(); if (dsChanged != null) { SqlCommandBuilder builder=new SqlCommandBuilder(da); //自动生成InsertCommand,UpdateCommand,DeleteCommand命令,前提是:Select语句为单表,同时此语句包含主键或唯一列 da.Update(ds,"Author");//Update方法检测DataSet中的每条记录,如果行状态不为Unchanaged,根据其行状态调用不同的SQL语句。 ds.AcceptChanges(); }
OleDbDataAdapter da = new OleDbDataAdapter(); OleDbCommand cmd = new OleDbCommand("insert into [A_Emp_Dept_20190522](empname,line) values (?,?)", conn); cmd.Parameters.Add(new OleDbParameter("empname", OleDbType.Char, 11, ParameterDirection.Input, false, 0, 0, "empname", DataRowVersion.Current, null)); cmd.Parameters.Add(new OleDbParameter("line", OleDbType.Char, 11, ParameterDirection.Input, false, 0, 0, "line", DataRowVersion.Current, null)); da.InsertCommand = cmd; //da.InsertCommand.UpdatedRowSource= UpdateRowSource.Both; DataSet ds = new DataSet(); DataRow row = ds.Tables[0].NewRow(); row["empname"] = "222"; row["line"] = "FBd"; ds.Tables[0].Rows.Add(row); da.Update(ds);