使用存储过程(具有连接)通过WCF服务c#填充DataGridView
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了使用存储过程(具有连接)通过WCF服务c#填充DataGridView,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2104字,纯文字阅读大概需要4分钟。
内容图文
我正在调用WCF服务,它为我提供了BAL中具有指定字段值的客户列表.
当我初始化它以在DataGridView中显示数据时,显示具有来自Customer表的相应数据类型值的所有内容(例如FirstName,LastName和Phone1).
但是,我想在存储过程中使用内部联接从Country表中检索的值不希望在Customer表中显示相应的值.
而错误是CountryId在Customer表中是DBNull,我该如何为这个特定的例子解决它.
这是我的绑定数据代码(适用于某些字段):
IHotRes res = new MHotServiceProvider().Service;
List<CustomerListItem> customerlist = res.GetCustomerListItem();
_ListData = ToDataTable(customerlist);
这是我在BAL中的方法:
public List<CustomerListItem> GetCustomerListItem()
{
List<CustomerListItem> customerlist = null;
CustomerListItem item = null;
using (CustomerTableAdapter adp = new CustomerTableAdapter())
{
using (DAL.dstCustomer.CustomerDataTable tbl = adp.GetCustomerDataList())
{
customerlist = new List<CustomerListItem>();
foreach (var row in tbl)
{
item = new CustomerListItem();
item.FirstName = row.FirstName;
item.LastName = row.LastName;
item.Phone1 = row.Phone1;
string mystring = row.CountryId.ToString(); //i tried to convert it to string but it still gives me the error that 'The value for column 'CountryId' in table 'Customer' is DBNull.'
item.CountryId = mystring;
//item.NationalityId = row.NationalityId;
customerlist.Add(item);
}
}
}
return customerlist;
}
这是我的存储过程:
CREATE PROCEDURE [dbo].[sp_CustomerDataList]
AS
BEGIN
SET NOCOUNT ON;
SELECT cu.FirstName, cu.LastName, cu.Phone1, co.CountryName, n.Nationality
FROM Customer cu
Left Join Country co
ON cu.CountryId = co.CountryId
Left Join Nationality n
ON cu.NationalityId = n.NationalityId
WHERE cu.IsDeleted = 0
END
如果需要更多代码示例,请告诉我们.
解决方法:
将CountryId添加到存储过程:
CREATE PROCEDURE [dbo].[sp_CustomerDataList]
AS
BEGIN
SET NOCOUNT ON;
SELECT cu.FirstName, cu.LastName, cu.Phone1, co.CountryId, co.CountryName, n.Nationality
FROM Customer cu
Left Join Country co
ON cu.CountryId = co.CountryId
Left Join Nationality n
ON cu.NationalityId = n.NationalityId
WHERE cu.IsDeleted = 0
END
然后将CountryName添加到模型中:
public class CustomerListItem
{
string CountryName = string.Empty;
...
}
内容总结
以上是互联网集市为您收集整理的使用存储过程(具有连接)通过WCF服务c#填充DataGridView全部内容,希望文章能够帮你解决使用存储过程(具有连接)通过WCF服务c#填充DataGridView所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。