本文整理汇总了C#中NpgsqlConnection类的典型用法代码示例。如果您正苦于以下问题:C# NpgsqlConnection类的具体用法?C# NpgsqlConnection怎么用?C# NpgsqlConnection使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
NpgsqlConnection类属于命名空间,在下文中一共展示了NpgsqlConnection类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C#代码示例。
示例1: ExecuteNonQuery
/// <summary>
/// 执行 Transact-SQL 语句并返回受影响的行数。
/// </summary>
public int ExecuteNonQuery(string connectionString, CommandType cmdType, string cmdText,
params DbParameter[] cmdParms)
{
NpgsqlCommand cmd = new NpgsqlCommand();
using (NpgsqlConnection conn = new NpgsqlConnection(connectionString))
{
PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
}
开发者ID:89sos98,项目名称:LBC,代码行数:16,代码来源:PostgreHelper.cs
示例2: LoadAggregatedChampionStatistics
List<AggregatedChampionStatistics> LoadAggregatedChampionStatistics(Summoner summoner, MapType map, GameModeType gameMode, NpgsqlConnection database)
{
const string query =
"with source as " +
"(select player.champion_id, player.won, player.kills, player.deaths, player.assists, player.gold, player.minion_kills from game_result, player where game_result.map = cast(:map as map_type) and game_result.game_mode = cast(:game_mode as game_mode_type) and (game_result.team1_id = player.team_id or game_result.team2_id = player.team_id) and player.summoner_id = :summoner_id) " +
"select statistics.champion_id, coalesce(champion_wins.wins, 0) as wins, coalesce(champion_losses.losses, 0) as losses, statistics.kills, statistics.deaths, statistics.assists, statistics.gold, statistics.minion_kills from " +
"(select source.champion_id, sum(source.kills) as kills, sum(source.deaths) as deaths, sum(source.assists) as assists, sum(source.gold) as gold, sum(source.minion_kills) as minion_kills from source group by source.champion_id) " +
"as statistics " +
"left outer join " +
"(select champion_id, count(*) as wins from source where won = true group by champion_id) " +
"as champion_wins " +
"on statistics.champion_id = champion_wins.champion_id " +
"left outer join " +
"(select champion_id, count(*) as losses from source where won = false group by champion_id) " +
"as champion_losses " +
"on statistics.champion_id = champion_losses.champion_id;";
DatabaseCommand select = GetCommand(query, database);
select.SetEnum("map", map.ToEnumString());
select.SetEnum("game_mode", gameMode.ToEnumString());
select.Set("summoner_id", summoner.Id);
using (NpgsqlDataReader reader = select.ExecuteReader())
{
List<AggregatedChampionStatistics> output = new List<AggregatedChampionStatistics>();
while (reader.Read())
{
AggregatedChampionStatistics statistics = new AggregatedChampionStatistics(reader);
statistics.ChampionName = GetChampionName(statistics.ChampionId);
output.Add(statistics);
}
output.Sort();
return output;
}
}
开发者ID:LeeSeungSoo,项目名称:RiotControl,代码行数:33,代码来源:Database.cs
示例3: UseAllConnectionsInPool
public void UseAllConnectionsInPool()
{
// As this method uses a lot of connections, clear all connections from all pools before starting.
// This is needed in order to not reach the max connections allowed and start to raise errors.
NpgsqlConnection.ClearAllPools();
try {
var openedConnections = new List<NpgsqlConnection>();
// repeat test to exersize pool
for (var i = 0; i < 10; ++i) {
try {
// 18 since base class opens two and the default pool size is 20
for (var j = 0; j < 18; ++j) {
var connection = new NpgsqlConnection(ConnectionString);
connection.Open();
openedConnections.Add(connection);
}
} finally {
openedConnections.ForEach(delegate(NpgsqlConnection con) { con.Dispose(); });
openedConnections.Clear();
}
}
} finally {
NpgsqlConnection.ClearAllPools();
}
}
开发者ID:Emill,项目名称:Npgsql,代码行数:26,代码来源:ConnectionPoolTests.cs
示例4: ResetOnClose
public void ResetOnClose()
{
var conn = new NpgsqlConnection(ConnectionString + ";SearchPath=public");
conn.Open();
ExecuteNonQuery("SET search_path=foo", conn);
conn.Close();
conn.Open();
Assert.That(ExecuteScalar("SHOW search_path", conn), Is.EqualTo("public"));
conn.Close();
}
开发者ID:ru-sh,项目名称:npgsql,代码行数:10,代码来源:ConnectionPoolTests.cs
示例5: MinPoolSize
public void MinPoolSize()
{
var conn = new NpgsqlConnection(ConnectionString + ";MinPoolSize=30;MaxPoolSize=30");
conn.Open();
conn.Close();
conn = new NpgsqlConnection(ConnectionString + ";MaxPoolSize=30;MinPoolSize=30");
conn.Open();
conn.Close();
}
开发者ID:Emill,项目名称:Npgsql,代码行数:10,代码来源:ConnectionPoolTests.cs
示例6: MinPoolSizeEqualsMaxPoolSize
public void MinPoolSizeEqualsMaxPoolSize()
{
using (var conn = new NpgsqlConnection(new NpgsqlConnectionStringBuilder(ConnectionString) {
MinPoolSize = 30,
MaxPoolSize = 30
}))
{
conn.Open();
}
}
开发者ID:ArsenShnurkov,项目名称:npgsql,代码行数:10,代码来源:PoolTests.cs
示例7: MinimumPgVersion
public static void MinimumPgVersion(NpgsqlConnection conn, string minVersion, string ignoreText=null)
{
var min = new Version(minVersion);
if (conn.PostgreSqlVersion < min)
{
var msg = $"Postgresql backend version {conn.PostgreSqlVersion} is less than the required {min}";
if (ignoreText != null)
msg += ": " + ignoreText;
Assert.Ignore(msg);
}
}
开发者ID:ArsenShnurkov,项目名称:npgsql,代码行数:11,代码来源:TestUtil.cs
示例8: MinPoolSizeLargeThanMaxPoolSize
public void MinPoolSizeLargeThanMaxPoolSize()
{
using (var conn = new NpgsqlConnection(new NpgsqlConnectionStringBuilder(ConnectionString)
{
MinPoolSize = 2,
MaxPoolSize = 1
}))
{
Assert.That(() => conn.Open(), Throws.Exception.TypeOf<ArgumentException>());
}
}
开发者ID:ArsenShnurkov,项目名称:npgsql,代码行数:11,代码来源:PoolTests.cs
示例9: ExecuteQuery
/// <summary>
/// 执行查询,返回DataSet
/// </summary>
public DataSet ExecuteQuery(string connectionString, CommandType cmdType, string cmdText,
params DbParameter[] cmdParms)
{
using (NpgsqlConnection conn = new NpgsqlConnection(connectionString))
{
using (NpgsqlCommand cmd = new NpgsqlCommand())
{
PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);
using (NpgsqlDataAdapter da = new NpgsqlDataAdapter(cmd))
{
DataSet ds = new DataSet();
da.Fill(ds, "ds");
cmd.Parameters.Clear();
return ds;
}
}
}
}
开发者ID:89sos98,项目名称:LBC,代码行数:21,代码来源:PostgreHelper.cs
示例10: RejectSelfSignedCertificate
public void RejectSelfSignedCertificate(bool useSslStream)
{
var csb = new NpgsqlConnectionStringBuilder(ConnectionString)
{
SslMode = SslMode.Require,
UseSslStream = useSslStream
};
using (var conn = new NpgsqlConnection(csb))
{
// The following is necessary since a pooled connector may exist from a previous
// SSL test
NpgsqlConnection.ClearPool(conn);
// TODO: Specific exception, align with SslStream
Assert.That(() => conn.Open(), Throws.Exception);
}
}
开发者ID:ArsenShnurkov,项目名称:npgsql,代码行数:18,代码来源:SecurityTests.cs
示例11: ExecuteReader
/// <summary>
/// 执行查询,返回DataReader
/// </summary>
public DbDataReader ExecuteReader(string connectionString, CommandType cmdType, string cmdText,
params DbParameter[] cmdParms)
{
NpgsqlCommand cmd = new NpgsqlCommand();
NpgsqlConnection conn = new NpgsqlConnection(connectionString);
try
{
PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);
NpgsqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
return rdr;
}
catch
{
conn.Close();
throw;
}
}
开发者ID:89sos98,项目名称:LBC,代码行数:22,代码来源:PostgreHelper.cs
示例12: ResetOnClose
public void ResetOnClose()
{
var conn = new NpgsqlConnection(ConnectionString + ";SearchPath=public");
conn.Open();
ExecuteNonQuery("DROP SCHEMA IF EXISTS foo");
ExecuteNonQuery("CREATE SCHEMA foo");
try
{
ExecuteNonQuery("SET search_path=foo", conn);
conn.Close();
conn.Open();
Assert.That(ExecuteScalar("SHOW search_path", conn), Is.EqualTo("public"));
conn.Close();
}
finally
{
ExecuteNonQuery("DROP SCHEMA foo");
}
}
开发者ID:Emill,项目名称:Npgsql,代码行数:19,代码来源:ConnectionPoolTests.cs
示例13: MinPoolSize
public void MinPoolSize()
{
var connString = new NpgsqlConnectionStringBuilder(ConnectionString) { MinPoolSize = 2 };
using (var conn = new NpgsqlConnection(connString))
{
connString = conn.Settings; // Shouldn't be necessary
conn.Open();
conn.Close();
}
var pool = PoolManager.Pools[connString];
Assert.That(pool.Idle, Has.Count.EqualTo(2));
// Now open 2 connections and make sure they're good
using (var conn1 = OpenConnection(connString))
using (var conn2 = OpenConnection(connString))
{
Assert.That(pool.Idle, Has.Count.Zero);
Assert.That(conn1.ExecuteScalar("SELECT 1"), Is.EqualTo(1));
Assert.That(conn2.ExecuteScalar("SELECT 1"), Is.EqualTo(1));
}
}
开发者ID:ArsenShnurkov,项目名称:npgsql,代码行数:22,代码来源:PoolTests.cs
示例14: GetAllUsers
//
// MembershipProvider.GetAllUsers
//
public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords)
{
NpgsqlConnection conn = new NpgsqlConnection(connectionString);
NpgsqlCommand cmd =
new NpgsqlCommand(string.Format("SELECT Count(*) FROM {0} WHERE application_name = @application_name", tableName), conn);
cmd.Parameters.Add("@application_name", NpgsqlDbType.Text, 255).Value = ApplicationName;
MembershipUserCollection users = new MembershipUserCollection();
NpgsqlDataReader reader = null;
totalRecords = 0;
try
{
conn.Open();
totalRecords = Convert.ToInt32(cmd.ExecuteScalar());
if (totalRecords <= 0)
{
return users;
}
cmd.CommandText = string.Format("SELECT UserId, user_name, Email, password_question, Comment, is_approved, is_locked_out, creation_date, last_login_date, last_activity_date, last_password_changed_date, last_locked_out_date FROM {0} WHERE application_name = @application_name ORDER BY user_name Asc", tableName);
using (reader = cmd.ExecuteReader())
{
int counter = 0;
int startIndex = pageSize*pageIndex;
int endIndex = startIndex + pageSize - 1;
while (reader.Read())
{
if (counter >= startIndex)
{
MembershipUser u = GetUserFromReader(reader);
users.Add(u);
}
if (counter >= endIndex)
{
cmd.Cancel();
}
counter++;
}
reader.Close();
}
}
catch (NpgsqlException e)
{
if (WriteExceptionsToEventLog)
{
WriteToEventLog(e, "GetAllUsers");
// use fully qualified name so as not to conflict with System.Data.ProviderException
// in System.Data.Entity assembly
throw new System.Configuration.Provider.ProviderException(exceptionMessage);
}
else
{
throw;// e;
}
}
finally
{
if (reader != null)
{
reader.Close();
}
cmd.Dispose();
conn.Close();
}
return users;
}
开发者ID:seeseekey,项目名称:CSCL,代码行数:76,代码来源:NpgsqlMembershipProvider.cs
示例15: MinPoolSizeLargeThanPoolSizeLimit
public void MinPoolSizeLargeThanPoolSizeLimit()
{
var conn = new NpgsqlConnection(ConnectionString + ";MinPoolSize=1025;");
conn.Open();
conn.Close();
}
开发者ID:Emill,项目名称:Npgsql,代码行数:6,代码来源:ConnectionPoolTests.cs
示例16: MinPoolSizeLargeThanMaxPoolSize
public void MinPoolSizeLargeThanMaxPoolSize()
{
var conn = new NpgsqlConnection(ConnectionString + ";MinPoolSize=2;MaxPoolSize=1");
conn.Open();
conn.Close();
}
开发者ID:Emill,项目名称:Npgsql,代码行数:6,代码来源:ConnectionPoolTests.cs
示例17: IntegratedSecurityWithUsername
public void IntegratedSecurityWithUsername()
{
var username = Environment.GetEnvironmentVariable("USERNAME") ??
Environment.GetEnvironmentVariable("USER");
if (username == null)
throw new Exception("Could find username");
var csb = new NpgsqlConnectionStringBuilder(ConnectionString) {
IntegratedSecurity = true,
Username = username,
Password = null,
};
using (var conn = new NpgsqlConnection(csb))
{
try
{
conn.Open();
}
catch (Exception e)
{
if (TestUtil.IsOnBuildServer)
throw;
Console.WriteLine(e);
Assert.Ignore("Integrated security (GSS/SSPI) doesn't seem to be set up");
}
}
}
开发者ID:ArsenShnurkov,项目名称:npgsql,代码行数:27,代码来源:SecurityTests.cs
示例18: IntegratedSecurityWithoutUsername
public void IntegratedSecurityWithoutUsername()
{
var csb = new NpgsqlConnectionStringBuilder(ConnectionString)
{
IntegratedSecurity = true,
Username = null,
Password = null,
};
using (var conn = new NpgsqlConnection(csb))
{
try
{
conn.Open();
}
catch (Exception e)
{
if (TestUtil.IsOnBuildServer)
throw;
Console.WriteLine(e);
Assert.Ignore("Integrated security (GSS/SSPI) doesn't seem to be set up");
}
}
}
开发者ID:ArsenShnurkov,项目名称:npgsql,代码行数:23,代码来源:SecurityTests.cs
示例19: ValidateUser
//
// MembershipProvider.ValidateUser
//
public override bool ValidateUser(string username, string password)
{
bool isValid = false;
NpgsqlConnection conn = new NpgsqlConnection(connectionString);
NpgsqlCommand cmd =
new NpgsqlCommand(
"SELECT Password, is_approved FROM " + tableName + "" +
" WHERE user_name = @user_name AND application_name = @application_name AND is_locked_out = false", conn);
cmd.Parameters.Add("@user_name", NpgsqlDbType.Text, 255).Value = username;
cmd.Parameters.Add("@application_name", NpgsqlDbType.Text, 255).Value = pApplicationName;
NpgsqlDataReader reader = null;
bool isApproved = false;
string pwd = "";
try
{
conn.Open();
using (reader = cmd.ExecuteReader(CommandBehavior.SingleRow))
{
if (reader.HasRows)
{
reader.Read();
pwd = reader.GetString(0);
isApproved = reader.GetBoolean(1);
}
else
{
return false;
}
reader.Close();
}
if (CheckPassword(password, pwd))
{
if (isApproved)
{
isValid = true;
NpgsqlCommand updateCmd =
new NpgsqlCommand(
"UPDATE " + tableName + " SET last_login_date = @last_login_date, last_activity_date = @last_activity_date" +
" WHERE user_name = @user_name AND application_name = @application_name", conn);
updateCmd.Parameters.Add("@last_login_date", NpgsqlDbType.Timestamp).Value = DateTime.Now;
updateCmd.Parameters.Add("@last_activity_date", NpgsqlDbType.Timestamp).Value = DateTime.Now;
// fixed by Alex .ToString("yyyy/MM/dd HH:mm:ss");
updateCmd.Parameters.Add("@user_name", NpgsqlDbType.Text, 255).Value = username;
updateCmd.Parameters.Add("@application_name", NpgsqlDbType.Text, 255).Value = pApplicationName;
updateCmd.ExecuteBlind();
}
}
else
{
cmd.Dispose();
conn.Close();
UpdateFailureCount(username, "password");
}
}
catch (NpgsqlException e)
{
if (WriteExceptionsToEventLog)
{
WriteToEventLog(e, "ValidateUser");
// use fully qualified name so as not to conflict with System.Data.ProviderException
// in System.Data.Entity assembly
throw new System.Configuration.Provider.ProviderException(exceptionMessage);
}
else
{
throw e;
}
}
finally
{
if (reader != null)
{
reader.Close();
}
cmd.Dispose();
conn.Close();
}
return isValid;
}
开发者ID:seeseekey,项目名称:CSCL,代码行数:95,代码来源:NpgsqlMembershipProvider.cs
示例20: GetUser
/// <summary>
/// MembershipProvider.GetUser(string, bool)
/// </summary>
/// <param name="username"></param>
/// <param name="userIsOnline"></param>
/// <returns></returns>
public override MembershipUser GetUser(string username, bool userIsOnline)
{
NpgsqlConnection conn = new NpgsqlConnection(connectionString);
NpgsqlCommand cmd =
new NpgsqlCommand(
string.Format("SELECT UserId, user_name, Email, password_question, Comment, is_approved, is_locked_out, creation_date, last_login_date, last_activity_date, last_password_changed_date, last_locked_out_date FROM {0} WHERE user_name = @user_name AND application_name = @application_name", tableName), conn);
cmd.Parameters.Add("@user_name", NpgsqlDbType.Text, 255).Value = username;
cmd.Parameters.Add("@application_name", NpgsqlDbType.Text, 255).Value = pApplicationName;
MembershipUser u = null;
NpgsqlDataReader reader = null;
try
{
conn.Open();
using (reader = cmd.ExecuteReader())
{
if (reader.HasRows)
{
reader.Read();
u = GetUserFromReader(reader);
reader.Close();
if (userIsOnline)
{
NpgsqlCommand updateCmd =
new NpgsqlCommand(
string.Format("UPDATE {0} SET last_activity_date = @last_activity_date WHERE user_name = @user_name AND application_name = @application_name", tableName), conn);
updateCmd.Parameters.Add("@last_activity_date", NpgsqlDbType.Timestamp).Value = DateTime.Now;
// fixed by Alex .ToString("yyyy/MM/dd HH:mm:ss");
updateCmd.Parameters.Add("@user_name", NpgsqlDbType.Text, 255).Value = username;
updateCmd.Parameters.Add("@application_name", NpgsqlDbType.Text, 255).Value = pApplicationName;
updateCmd.ExecuteBlind();
}
}
reader.Close();
}
}
catch (NpgsqlException e)
{
if (WriteExceptionsToEventLog)
{
WriteToEventLog(e, "GetUser(String, Boolean)");
// use fully qualified name so as not to conflict with System.Data.ProviderException
// in System.Data.Entity assembly
throw new System.Configuration.Provider.ProviderException(exceptionMessage);
}
else
{
throw e;
}
}
finally
{
if (reader != null)
{
reader.Close();
}
cmd.Dispose();
conn.Close();
}
return u;
}
开发者ID:seeseekey,项目名称:CSCL,代码行数:76,代码来源:NpgsqlMembershipProvider.cs
注:本文中的NpgsqlConnection类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论