[DevExpress WinForms控件] 如何绑定到SQL数据?(三)

想要绑定到SQL数据有三种方法可用: ?通过数据源配置向导绑定 ?通过SqlDataSource组件手动绑定 ?用代码绑定到SQL数据 本系列教程将为您一一介绍。

DevExpress线下研修班

三、使用代码绑定到SQL数据

以下代码说明了如何使用SqlDataSource组件在运行时将GridControl绑定到SQL数据。

在此示例中,使用的是CustomSqlQuery。要了解其他查询类型,请参阅SelectQuery和StoredProcQuery类的说明。

要了解支持的数据库引擎,请参阅DataConnectionParametersBase的继承层次结构。

[C#]

using DevExpress.DataAccess.ConnectionParameters;using DevExpress.DataAccess.Sql;// . . .// Create a data source.Access97ConnectionParameters connectionParameters = new Access97ConnectionParameters("D:\Work\nwind.mdb", "", "");SqlDataSource ds = new SqlDataSource(connectionParameters);// Create an SQL query to access the Products table.CustomSqlQuery query = new CustomSqlQuery();query.Name = "customQuery1";query.Sql = "SELECT [Products].[ProductID], [Products].[ProductName], [Products].[QuantityPerUnit], [Products].[UnitPrice], "    + "[Products].[UnitsInStock], [Products].[UnitsOnOrder], [Products].[ReorderLevel], [Products].[Discontinued] "    +"FROM [Products] [Products]";ds.Queries.Add(query);ds.Fill();//Assign the data source for the grid and retrieve fields.gridControl1.DataSource = ds;gridControl1.DataMember = "customQuery1";

[VB]

Imports DevExpress.DataAccess.ConnectionParametersImports DevExpress.DataAccess.Sql' . . . ' Create a data source. Private connectionParameters As New Access97ConnectionParameters("D:Worknwind.mdb", "", "")Private ds As New SqlDataSource(connectionParameters)' Create an SQL query to access the Products table. Private query As New CustomSqlQuery()query.Name = "customQuery1"query.Sql = "SELECT [Products].[ProductID], [Products].[ProductName], [Products].[QuantityPerUnit], [Products].[UnitPrice], " & "[Products].[UnitsInStock], [Products].[UnitsOnOrder], [Products].[ReorderLevel], [Products].[Discontinued] " & "FROM [Products] [Products]" ds.Queries.Add(query)ds.Fill()'Assign the data source for the grid and retrieve fields. gridControl1.DataSource = dsgridControl1.DataMember = "customQuery1"

请注意,在控件绑定到数据源之后,具体控件可能需要其他自定义。有关更多信息,请参阅相应控件的文档。

学院2017全新DevExpress线下研修班火热 名中!

  • WinForms控件学习+实践的不二之选——DevExpress WinForm提升班
  • 高效的MVVM开发模式,WPF开发首选培训平台——DevExpress WPF提升班
  • 手把手将您快速开发出完美、强大的ASP应用程序——DevExpress ASP.NET提升班

标签:DevExpressSQLWinForms

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2017年8月2日
下一篇 2017年8月2日

相关推荐

发表回复

登录后才能评论