Основы ASP.NET 2.0



         

DataSet - часть 2


public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { String strConnect; String strCommand; strConnect = @"Data Source=.\SQLExpress;Initial Catalog=Northwind;Integrated Security=True"; SqlConnection myConn = new SqlConnection(strConnect); strCommand = "SELECT CategoryName, CategoryID FROM Categories"; SqlDataAdapter myData = new SqlDataAdapter(strCommand, myConn);

DataSet DataSet1 = new DataSet(); myData.Fill(DataSet1, "Categories"); strCommand = "SELECT ProductName, UnitPrice, CategoryID FROM Products"; myData.SelectCommand.CommandText = strCommand; myData.Fill(DataSet1, "Products"); DataSet1.Relations.Add(DataSet1.Tables[0].Columns["CategoryID"], DataSet1.Tables[1].Columns["CategoryID"]);

DataView myView = new DataView(DataSet1.Tables["Products"], "", "ProductName", DataViewRowState.CurrentRows); DataGrid1.DataSource = myView; DataGrid1.DataBind(); DropDownList1.DataSource = DataSet1.Tables[0]; DropDownList1.DataTextField = "CategoryName"; DropDownList1.DataValueField = "CategoryID"; DropDownList1.DataBind(); myConn.Close();

} }




Содержание  Назад  Вперед