Reputation: 35
I'm new to wcf and I am trying to use wcf connect to sql and display values in a datagridview, how to do it?
In IProductsService.cs, I just did
[OperationContract]
Products[] getProducts();
and in Products.cs
[DataMember]
string productId, productName, location;
public Products(string productId1, string productName1, string location1)
{
this.productId = productId1;
this.productName = productName1;
this.location = location1;
}
ProductsService.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;
}
}
Upvotes: 1
Views: 713
Reputation: 1347
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;
}
Upvotes: 2