Combine multiple data sheets

xiaoxiao2021-03-06  41

private System.Data.SqlClient.SqlConnection myConnection; private System.Data.SqlClient.SqlCommand myCommand; private System.Data.SqlClient.SqlCommand myCommand2; private System.Data.DataSet myDataSet; private System.Data.SqlClient.SqlDataAdapter myDataAdapter; private System .Data.SqlClient.SqlDataAdapter myDataAdapter2; // create a connection object and open its string connectionString = "server = .; uid = sa; pwd =; database = northwind"; myConnection = new System.Data.SqlClient.SqlConnection (connectionString); MyConnection.open ();

// Create a DataSet and set its nature myDataSet = new system.data.dataset (); myDataSet.casensITIVE = true; // Create a sqlcommand object and assign a value to it to connect objects and SELECT statement myCommand = new system.data.sqlclient.sqlcommand (); Mycommand.connection = myconnection; mycommand.commandtext = "Select * from customers";

MyCommand2 = new system.data.sqlclient.sqlcommand (); mycommand2.connection = myconnection; mycommand2.commandtext = "select * from orderers";

// Create a DataAdapter object and pass SqlCommand object, and create a table insinuate myDataAdapter = new System.Data.SqlClient.SqlDataAdapter (); myDataAdapter.SelectCommand = myCommand; myDataAdapter.TableMappings.Add ( "Table", "Customers");

MyDataAdapter2 = new system.data.sqlclient.sqldataadapter (); mydataadapter2.selectCommand = mycommand2; mydataadapter2.tablemappings.add ("Table", "ORDERS");

MyDataAdapter.Fill (MyDataSet); MyDataAdapter2.fill (MyDataSet);

// Create the relationship between the table System.Data.DataRelation DataRelelation; System.Data.Datacolumn Datacolumn1; System.Data.Datacolumn Datacolumn2;

dataColumn1 = myDataSet.Tables [ "Customers"] Columns [ "CustomerID"];.. dataColumn2 = myDataSet.Tables [ "Orders"] Columns [ "CustomerID"]; dataRelation = new System.Data.DataRelation ( "CustomersToOrders", dataColumn1 , dataColumn2); myDataSet.Relations.Add (dataRelation); DataViewManager dataSetView = myDataSet.DefaultViewManager; customersDataGrid.DataSource = dataSetView; customersDataGrid.DataMember = "Customers";

转载请注明原文地址:https://www.9cbs.com/read-77324.html

New Post(0)