DataSet Relastion (1)
来源:互联网 发布:数控锥螺纹怎么编程 编辑:程序博客网 时间:2024/05/21 22:48
因用一个小程序实现 dataset 各个表的关系。用到了relastion 先创建。
private static void ConnectToData(string connectionString) { //Create a SqlConnection to the Northwind database. using (SqlConnection connection = new SqlConnection(connectionString)) { //Create a SqlDataAdapter for the Suppliers table. SqlDataAdapter adapter = new SqlDataAdapter(); // A table mapping names the DataTable. adapter.TableMappings.Add("Table", "Suppliers"); // Open the connection. connection.Open(); Console.WriteLine("The SqlConnection is open."); // Create a SqlCommand to retrieve Suppliers data. SqlCommand command = new SqlCommand( "SELECT SupplierID, CompanyName FROM dbo.Suppliers;", connection); command.CommandType = CommandType.Text; // Set the SqlDataAdapter's SelectCommand. adapter.SelectCommand = command; // Fill the DataSet. DataSet dataSet = new DataSet("Suppliers"); adapter.Fill(dataSet); // Create a second Adapter and Command to get // the Products table, a child table of Suppliers. SqlDataAdapter productsAdapter = new SqlDataAdapter(); productsAdapter.TableMappings.Add("Table", "Products"); SqlCommand productsCommand = new SqlCommand( "SELECT ProductID, SupplierID FROM dbo.Products;", connection); productsAdapter.SelectCommand = productsCommand; // Fill the DataSet. productsAdapter.Fill(dataSet); // Close the connection. connection.Close(); Console.WriteLine("The SqlConnection is closed."); // Create a DataRelation to link the two tables // based on the SupplierID. DataColumn parentColumn = dataSet.Tables["Suppliers"].Columns["SupplierID"]; DataColumn childColumn = dataSet.Tables["Products"].Columns["SupplierID"]; DataRelation relation = new System.Data.DataRelation("SuppliersProducts", parentColumn, childColumn); dataSet.Relations.Add(relation); Console.WriteLine( "The {0} DataRelation has been created.", relation.RelationName); } }
没有看晕吧
其实 就一句话
getDS.Relations.Add("plan1", getDS.Tables[0].Columns["plan_Id"], getDS.Tables[1].Columns["plan_Id"]);
就可以了。 如果报错 提示什么关系已存在了。 你就新建立一个dataset 然后 再建立relastion
- DataSet Relastion (1)
- DataSet (1)
- Linq to DataSet 1
- 操作DataSet(赋值)
- 构造DataSet(记录)
- DataSet 类 (一)
- DataSet
- DataSet
- DataSet
- DataSet
- DataSet
- dataset
- DataSet
- DataSet
- DataSet
- dataSet
- DataSet
- DataSet
- CSS3 HTML5实例三(块阴影与文字阴影)
- 封装一下System.out.print()吧
- 使用yum来安装或卸载CentOS图形界面包
- 插件insertpre+语法高亮插件google-code-prettify,解决CKEditor4代码高亮显示问题
- 输出Unity的场景文件为Obj模型文件
- DataSet Relastion (1)
- SAP业务智能是一个企业级的、完整的、开放的和集成的方案,提供可操作的洞察力。
- 对领域驱动设计(DDD)的学习成果
- eclipse编程问题!!
- 正则表达式
- OpenGL离线渲染和缓冲区对象
- 如何在Linux中安装tomcat,部署Java web项目
- loadrunner中使用zlib.dll压缩或解压
- 做一个女软件工程师的好处