C# 如何在 DataGridView 的 Windows 窗体应用中显示 WCF 服务的 SQL 值
本文关键字:显示 WCF 服务 SQL 应用 窗体 DataGridView Windows | 更新日期: 2023-09-27 18:36:20
我是wcf的新手,我正在尝试使用wcf连接到sql并在datagridview中显示值,该怎么做?
在IProductsService.cs中,我只是做了
[OperationContract]
Products[] getProducts();
和产品.cs
[DataMember]
string productId, productName, location;
public Products(string productId1, string productName1, string location1)
{
this.productId = productId1;
this.productName = productName1;
this.location = location1;
}
产品服务.svc.cs
namespace Products
{
public Products[] getProducts()
{
SqlConnection connection = new SqlConnection( "Data Source=111111;Initial Catalog=11111;User ID=111111;Password=11111");
connection.Open();
SqlCommand command = new SqlCommand("SELECT productId, productName, location FROM Products", connection);
SqlDataReader reader = command.ExecuteReader();
//Do something here to send value? how?
return null;
}
}
public List<Prodict> getProducts()
{
SqlConnection connection = new SqlConnection( "Data Source=111111;Initial Catalog=11111;User ID=111111;Password=11111");
connection.Open();
SqlCommand command = new SqlCommand("SELECT productId, productName, location FROM Products", connection);
var productList = new List<Product>(); //Do Array, if it is better for you.
using(SqlDataReader rdr = cmd.ExecuteReader())
{
while (rdr.Read())
{
var product = new Product(rdr.GetValue(0).ToString(),rdr.GetValue(1).ToString(),rdr.GetValue(2).ToString());
productList.Add(product);
}
}
return productList;
}