https://github.com/karenpayneoregon/csharpworkingwithoutparamswithdata
TechNet: C# 7 new methods for returning data with out parameters and ValueTuple source code
https://github.com/karenpayneoregon/csharpworkingwithoutparamswithdata
csharp
Last synced: over 1 year ago
JSON representation
TechNet: C# 7 new methods for returning data with out parameters and ValueTuple source code
- Host: GitHub
- URL: https://github.com/karenpayneoregon/csharpworkingwithoutparamswithdata
- Owner: karenpayneoregon
- Created: 2018-07-26T12:29:10.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2022-12-24T16:39:27.000Z (over 3 years ago)
- Last Synced: 2025-01-29T00:30:13.495Z (over 1 year ago)
- Topics: csharp
- Language: C#
- Homepage:
- Size: 91.8 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
TechNet: C# 7 new methods for returning data with out parameters and ValueTuple
ValueTuple
```csharp
public async Task<(bool Success, List Customers)> GetSCustomersUsingTuplesAsync()
{
mHasException = false;
var results = await Task.Run(() =>
{
mHasException = false;
var customersList = new List();
const string selectStatement =
"SELECT cust.CustomerIdentifier,cust.CompanyName,cust.ContactName,ct.ContactTitle, " +
"cust.[Address] AS street,cust.City,ISNULL(cust.PostalCode,''),cust.Country,cust.Phone, " +
"cust.ContactTypeIdentifier FROM dbo.Customers AS cust " +
"INNER JOIN ContactType AS ct ON cust.ContactTypeIdentifier = ct.ContactTypeIdentifier;";
using (var cn = new SqlConnection() { ConnectionString = ConnectionString })
{
using (var cmd = new SqlCommand() { Connection = cn, CommandText = selectStatement })
{
try
{
cn.Open();
var reader = cmd.ExecuteReader();
while (reader.Read())
{
customersList.Add(new Customer()
{
CustomerIdentifier = reader.GetInt32(0),
CompanyName = reader.GetString(1),
ContactName = reader.GetString(2),
ContactTitle = reader.GetString(3),
Street = reader.GetString(4),
City = reader.GetString(5),
PostalCode = reader.GetString(6),
Country = reader.GetString(7),
Phone = reader.GetString(8)
});
}
}
catch (Exception e)
{
mHasException = true;
mLastException = e;
}
}
}
// IsSuccessFul equates to returning all data, no data or partial data
// customerList will contain, no customers, some customers, all customers
return (IsSuccessFul, customersList);
}).ConfigureAwait(false);
return results;
}
```
Out parameter
```csharp
public bool Customers3(out List Customers)
{
mHasException = false;
Customers = new List();
const string selectStatement =
"SELECT cust.CustomerIdentifier,cust.CompanyName,cust.ContactName,ct.ContactTitle, " +
"cust.[Address] AS street,cust.City,ISNULL(cust.PostalCode,''),cust.Country,cust.Phone, " +
"cust.ContactTypeIdentifier FROM dbo.Customers AS cust " +
"INNER JOIN ContactType AS ct ON cust.ContactTypeIdentifier = ct.ContactTypeIdentifier;";
using (var cn = new SqlConnection() { ConnectionString = ConnectionString })
{
using (var cmd = new SqlCommand() { Connection = cn, CommandText = selectStatement })
{
try
{
cn.Open();
var reader = cmd.ExecuteReader();
while (reader.Read())
{
Customers.Add(new Customer()
{
CustomerIdentifier = reader.GetInt32(0),
CompanyName = reader.GetString(1),
ContactName = reader.GetString(2),
ContactTitle = reader.GetString(3),
Street = reader.GetString(4),
City = reader.GetString(5),
PostalCode = reader.GetString(6),
Country = reader.GetString(7),
Phone = reader.GetString(8)
});
}
}
catch (Exception e)
{
mHasException = true;
mLastException = e;
}
}
}
return IsSuccessFul;
}
```