本文整理汇总了C#中Npgsql.NpgsqlCommand.ExecuteScalarAsync方法的典型用法代码示例。如果您正苦于以下问题:C# NpgsqlCommand.ExecuteScalarAsync方法的具体用法?C# NpgsqlCommand.ExecuteScalarAsync怎么用?C# NpgsqlCommand.ExecuteScalarAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Npgsql.NpgsqlCommand
的用法示例。
在下文中一共展示了NpgsqlCommand.ExecuteScalarAsync方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Scalar
public async Task Scalar()
{
using (var conn = OpenConnection())
using (var cmd = new NpgsqlCommand("SELECT 1", conn)) {
Assert.That(await cmd.ExecuteScalarAsync(), Is.EqualTo(1));
}
}
示例2: ExecuteScalarAsync
protected async Task<object> ExecuteScalarAsync(string sql, NpgsqlConnection conn = null)
{
if (conn == null)
conn = Conn;
using (var cmd = new NpgsqlCommand(sql, conn))
return await cmd.ExecuteScalarAsync();
}
示例3: Scalar
public async void Scalar()
{
using (var cmd = new NpgsqlCommand("SELECT 1", Conn)) {
Assert.That(await cmd.ExecuteScalarAsync(), Is.EqualTo(1));
}
}
示例4: AddDriverAsync
public async Task<int> AddDriverAsync(Driver theDriver)
{
try
{
await Connection.OpenAsync().ConfigureAwait(false);
var aCommand = new NpgsqlCommand(
"Insert into driver (firstname, lastname, address, city, state, postalcode, country, licensenumber, licensestate, customerid) VALUES (:value1, :value2, :value3, :value4, :value5, :value6, :value7, :value8, :value9, :value10) RETURNING id", Connection);
aCommand.Parameters.AddWithValue("value1", theDriver.FirstName);
aCommand.Parameters.AddWithValue("value2", theDriver.LastName);
aCommand.Parameters.AddWithValue("value3", theDriver.Address);
aCommand.Parameters.AddWithValue("value4", theDriver.City);
aCommand.Parameters.AddWithValue("value5", theDriver.State);
aCommand.Parameters.AddWithValue("value6", theDriver.PostalCode);
aCommand.Parameters.AddWithValue("value7", theDriver.Country);
aCommand.Parameters.AddWithValue("value8", theDriver.LicenseNumber);
aCommand.Parameters.AddWithValue("value9", theDriver.LicenseState);
aCommand.Parameters.AddWithValue("value10", theDriver.CustomerId);
// returns the id from the SELECT, RETURNING sql statement above
return Convert.ToInt32(await aCommand.ExecuteScalarAsync().ConfigureAwait(false));
}
catch (NpgsqlException)
{
return 0;
}
catch (InvalidOperationException)
{
return 0;
}
catch (SqlException)
{
return 0;
}
catch (ConfigurationErrorsException)
{
return 0;
}
finally
{
if (Connection.State == ConnectionState.Open)
Connection.Close();
}
}
示例5: AddCustomerAsync
public async Task<int> AddCustomerAsync(Customer theCustomer)
{
try
{
await Connection.OpenAsync().ConfigureAwait(false);
var aCommand = new NpgsqlCommand(
"Insert into customer (name, allowsadditionaldrivers, allowsadditions, hasmaxrentaldays, maxrentaldays) VALUES (:value1, :value2, :value3, :value4, :value5) RETURNING id", Connection);
aCommand.Parameters.AddWithValue("value1", theCustomer.Name);
aCommand.Parameters.AddWithValue("value2", theCustomer.AllowsAdditionalDrivers);
aCommand.Parameters.AddWithValue("value3", theCustomer.AllowsAdditions);
aCommand.Parameters.AddWithValue("value4", theCustomer.HasMaxRentalDays);
aCommand.Parameters.AddWithValue("value5", theCustomer.MaxRentalDays);
// returns the id from the SELECT, RETURNING sql statement above
return Convert.ToInt32(await aCommand.ExecuteScalarAsync().ConfigureAwait(false));
}
catch (NpgsqlException)
{
return 0;
}
catch (InvalidOperationException)
{
return 0;
}
catch (SqlException)
{
return 0;
}
catch (ConfigurationErrorsException)
{
return 0;
}
finally
{
if (Connection.State == ConnectionState.Open)
Connection.Close();
}
}
示例6: TestConnectionAsync
/// <summary>
/// Tests the connection asynchronously.
/// </summary>
/// <returns></returns>
public override async Task TestConnectionAsync()
{
using (var con = await CreateConnectionAsync())
using (var cmd = new NpgsqlCommand("SELECT 1", con))
await cmd.ExecuteScalarAsync();
}
示例7: AddLocationAsync
public async Task<int> AddLocationAsync(Location theLocation)
{
try
{
await Connection.OpenAsync().ConfigureAwait(false);
var aCommand = new NpgsqlCommand(
"Insert into location (customerid, name, address, city, state, postalcode, country, latitude, longitude) VALUES (:value1, :value2, :value3, :value4, :value5, :value6, :value7, :value8, :value9) RETURNING id", Connection);
aCommand.Parameters.AddWithValue("value1", theLocation.CustomerId);
aCommand.Parameters.AddWithValue("value2", theLocation.Name);
aCommand.Parameters.AddWithValue("value3", theLocation.Address);
aCommand.Parameters.AddWithValue("value4", theLocation.City);
aCommand.Parameters.AddWithValue("value5", theLocation.State);
aCommand.Parameters.AddWithValue("value6", theLocation.PostalCode);
aCommand.Parameters.AddWithValue("value7", theLocation.Country);
aCommand.Parameters.AddWithValue("value8", theLocation.Latitude);
aCommand.Parameters.AddWithValue("value9", theLocation.Longitude);
// returns the id from the SELECT, RETURNING sql statement above
return Convert.ToInt32(await aCommand.ExecuteScalarAsync().ConfigureAwait(false));
}
catch (NpgsqlException)
{
return 0;
}
catch (InvalidOperationException)
{
return 0;
}
catch (SqlException)
{
return 0;
}
catch (ConfigurationErrorsException)
{
return 0;
}
finally
{
if (Connection.State == ConnectionState.Open)
Connection.Close();
}
}
示例8: AddUserAsync
public async Task<int> AddUserAsync(User theUser)
{
try
{
await Connection.OpenAsync().ConfigureAwait(false);
var aCommand = new NpgsqlCommand(
"Insert into appuser (firstname, lastname, email, customerid, isemployee) VALUES (:value1, :value2, :value3, :value4, :value5) RETURNING id", Connection);
aCommand.Parameters.AddWithValue("value1", theUser.FirstName);
aCommand.Parameters.AddWithValue("value2", theUser.LastName);
aCommand.Parameters.AddWithValue("value3", theUser.Email);
aCommand.Parameters.AddWithValue("value4", theUser.CustomerId);
aCommand.Parameters.AddWithValue("value5", theUser.IsEmployee);
// returns the id from the SELECT, RETURNING sql statement above
return Convert.ToInt32(await aCommand.ExecuteScalarAsync().ConfigureAwait(false));
}
catch (NpgsqlException)
{
return 0;
}
catch (InvalidOperationException)
{
return 0;
}
catch (SqlException)
{
return 0;
}
catch (ConfigurationErrorsException)
{
return 0;
}
finally
{
if (Connection.State == ConnectionState.Open)
Connection.Close();
}
}
示例9: AddAutomobileAsync
public async Task<int> AddAutomobileAsync(Automobile theAutomobile)
{
try
{
await Connection.OpenAsync().ConfigureAwait(false);
var aCommand = new NpgsqlCommand(
"Insert into automobile (vin, vehiclenumber, name, class, style, color, manufacturer, model, code, locationid) VALUES (:value1, :value2, :value3, :value4, :value5, :value6, :value7, :value8, :value9, :value10) RETURNING id", Connection);
aCommand.Parameters.AddWithValue("value1", theAutomobile.VIN);
aCommand.Parameters.AddWithValue("value2", theAutomobile.VehicleNumber);
aCommand.Parameters.AddWithValue("value3", theAutomobile.Name);
aCommand.Parameters.AddWithValue("value4", theAutomobile.Class);
aCommand.Parameters.AddWithValue("value5", theAutomobile.Style);
aCommand.Parameters.AddWithValue("value6", theAutomobile.Color);
aCommand.Parameters.AddWithValue("value7", theAutomobile.Manufacturer);
aCommand.Parameters.AddWithValue("value8", theAutomobile.Model);
aCommand.Parameters.AddWithValue("value9", theAutomobile.Code);
aCommand.Parameters.AddWithValue("value10", theAutomobile.LocationId);
// returns the id from the SELECT, RETURNING sql statement above
return Convert.ToInt32(await aCommand.ExecuteScalarAsync().ConfigureAwait(false));
}
catch (NpgsqlException)
{
return 0;
}
catch (InvalidOperationException)
{
return 0;
}
catch (SqlException)
{
return 0;
}
catch (ConfigurationErrorsException)
{
return 0;
}
finally
{
if (Connection.State == ConnectionState.Open)
Connection.Close();
}
}