当前位置: 首页>>代码示例>>C#>>正文


C# SqlConnection.OpenAsync方法代码示例

本文整理汇总了C#中SqlConnection.OpenAsync方法的典型用法代码示例。如果您正苦于以下问题:C# SqlConnection.OpenAsync方法的具体用法?C# SqlConnection.OpenAsync怎么用?C# SqlConnection.OpenAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在SqlConnection的用法示例。


在下文中一共展示了SqlConnection.OpenAsync方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: GetDetailsForFileById

        /// <inheritdoc/>
        public async Task<FileDetails> GetDetailsForFileById(int id)
        {
            FileDetails details = null;

            using (var connection = new SqlConnection(connectionString))
            {
                using (var command = new SqlCommand("GetFileMetaDatabyId", connection))
                {
                    command.CommandType = CommandType.StoredProcedure;

                    command.Parameters.AddWithValue("Id", id);

                    await connection.OpenAsync();

                    using (var reader = await command.ExecuteReaderAsync())
                    {
                        if (await reader.ReadAsync())
                        {
                            details = new FileDetails();
                            details.ApplicationName = reader["ApplicationName"].ToString();
                            details.FileName = reader["FileName"].ToString();
                            details.FileId = (int)reader["Id"];
                            details.MimeType = reader["MimeType"].ToString();
                            details.StoragePath = reader["InternalStoragePath"].ToString();
                            details.Checksum = reader["Checksum"].ToString();
                            details.DateStoredUtc = (DateTime)reader["DateStoredUtc"];
                        }
                    }
                }
            }

            return details;
        }
开发者ID:NaosProject,项目名称:Naos.FileStorage,代码行数:34,代码来源:FileMetaDataRepository.cs

示例2: ExecuteCommandWithNewConnectionAsync

        private static async Task ExecuteCommandWithNewConnectionAsync(string processName, string cmdText, ICollection<string> executedProcessList)
        {
            var conn = new SqlConnection(DataTestClass.SQL2005_Northwind);

            await conn.OpenAsync();
            var cmd = new SqlCommand(cmdText, conn);


            using (SqlDataReader reader = await cmd.ExecuteReaderAsync(CommandBehavior.CloseConnection))
            {
                while (await reader.ReadAsync())
                {
                    executedProcessList.Add(processName);
                }
            }
        }
开发者ID:shiftkey-tester,项目名称:corefx,代码行数:16,代码来源:AsyncTest.cs

示例3: SendMessage

 public static async Task SendMessage(string connectionString, string queue, string messageBody, Dictionary<string, string> headers)
 {
     var insertSql = [email protected]"INSERT INTO [{queue}] (
             [Id],
             [Recoverable],
             [Headers],
             [Body])
         VALUES (
             @Id,
             @Recoverable,
             @Headers,
             @Body)";
     using (var scope = new TransactionScope())
     {
         using (var connection = new SqlConnection(connectionString))
         {
             await connection.OpenAsync()
                 .ConfigureAwait(false);
             using (var command = new SqlCommand(insertSql, connection))
             {
                 var parameters = command.Parameters;
                 command.CommandType = CommandType.Text;
                 parameters.Add("Id", SqlDbType.UniqueIdentifier).Value = Guid.NewGuid();
                 var serializeHeaders = Newtonsoft.Json.JsonConvert.SerializeObject(headers);
                 parameters.Add("Headers", SqlDbType.VarChar).Value = serializeHeaders;
                 parameters.Add("Body", SqlDbType.VarBinary).Value = Encoding.UTF8.GetBytes(messageBody);
                 parameters.Add("Recoverable", SqlDbType.Bit).Value = true;
                 await command.ExecuteNonQueryAsync()
                     .ConfigureAwait(false);
             }
         }
         scope.Complete();
     }
 }
开发者ID:chriscatilo,项目名称:docs.particular.net,代码行数:34,代码来源:NativeSend.cs

示例4: PlaceOrder

    static async Task PlaceOrder()
    {
        #region MessagePayload

        var message = @"{
                           $type: 'PlaceOrder',
                           OrderId: 'Order from ADO.net sender'
                        }";

        #endregion

        #region SendingUsingAdoNet

        var connectionString = @"Data Source=.\SQLEXPRESS;Initial Catalog=samples;Integrated Security=True";
        using (var connection = new SqlConnection(connectionString))
        {
            await connection.OpenAsync()
                .ConfigureAwait(false);
            var insertSql = "INSERT INTO [Samples.SqlServer.NativeIntegration] ([Id],[Recoverable],[Headers],[Body]) VALUES (@Id,@Recoverable,@Headers,@Body)";
            using (var command = new SqlCommand(insertSql, connection))
            {
                command.CommandType = CommandType.Text;
                command.Parameters.Add("Id", SqlDbType.UniqueIdentifier).Value = Guid.NewGuid();
                command.Parameters.Add("Headers", SqlDbType.VarChar).Value = "";
                command.Parameters.Add("Body", SqlDbType.VarBinary).Value = Encoding.UTF8.GetBytes(message);
                command.Parameters.Add("Recoverable", SqlDbType.Bit).Value = true;
                await command.ExecuteNonQueryAsync()
                    .ConfigureAwait(false);
            }
        }

        #endregion
    }
开发者ID:chriscatilo,项目名称:docs.particular.net,代码行数:33,代码来源:Program.cs

示例5: CreateQueuesForEndpoint

    public async Task CreateQueuesForEndpoint()
    {
        var connectionString = @"Data Source=.\SQLEXPRESS;Initial Catalog=samples;Integrated Security=True";
        using (var sqlConnection = new SqlConnection(connectionString))
        {
            await sqlConnection.OpenAsync()
                .ConfigureAwait(false);
            await QueueCreation.CreateQueuesForEndpoint(
                connection: sqlConnection,
                schema: "dbo",
                endpointName: "myendpoint")
                .ConfigureAwait(false);

            await QueueCreation.CreateQueue(
                connection: sqlConnection,
                schema: "dbo",
                queueName: "error")
                .ConfigureAwait(false);

            await QueueCreation.CreateQueue(
                connection: sqlConnection,
                schema: "dbo",
                queueName: "audit")
                .ConfigureAwait(false);
        }

    }
开发者ID:chriscatilo,项目名称:docs.particular.net,代码行数:27,代码来源:QueueCreationTests.cs

示例6: GetConnection

    public static async Task<SqlConnection> GetConnection(string transportAddress)
    {
        string connectionString = transportAddress.Equals("[email protected][dbo]") 
                                                ? ReceiverConnectionString : SenderConnectionString;
        SqlConnection connection = new SqlConnection(connectionString);

        await connection.OpenAsync();

        return connection;
    }
开发者ID:odelljl,项目名称:docs.particular.net,代码行数:10,代码来源:ConnectionProvider.cs

示例7: Invoke

    public async Task<object> Invoke(IDictionary<string, object> parameters)
    {
        var commandText = (string)parameters["source"];
        var connectionString = (string)parameters["connectionString"];
        var dataSets = new List<List<object>>();
        var rows = new List<object>();

        using (var connection = new SqlConnection(connectionString))
        {
            using (var command = new SqlCommand(commandText, connection))
            {
                await connection.OpenAsync();
                using (var reader = await command.ExecuteReaderAsync(CommandBehavior.CloseConnection))
                {
                    var record = (IDataRecord)reader;
                    do
                    {
                        while (await reader.ReadAsync())
                        {
                            var dataObject = new ExpandoObject() as IDictionary<string, Object>;
                            var resultRecord = new object[record.FieldCount];
                            record.GetValues(resultRecord);

                            for (int i = 0; i < record.FieldCount; i++)
                            {
                                var type = record.GetFieldType(i);
                                if (type == typeof(byte[]) || type == typeof(char[]))
                                {
                                    resultRecord[i] = Convert.ToBase64String((byte[])resultRecord[i]);
                                }
                                else if (type == typeof(Guid) || type == typeof(DateTime))
                                {
                                    resultRecord[i] = resultRecord[i].ToString();
                                }
                                else if (type == typeof(IDataReader))
                                {
                                    resultRecord[i] = "<IDataReader>";
                                }

                                dataObject.Add(record.GetName(i), resultRecord[i]);
                            }
                            rows.Add(dataObject);
                        }
                        dataSets.Add(rows);
                        rows = new List<object>();
                    }
                    while(await reader.NextResultAsync());
                }
            }
        }

        return dataSets;
    }
开发者ID:Wintellect,项目名称:SqlServerGadgetBox,代码行数:53,代码来源:SqlDataReader.cs

示例8: ExecuteQuery

    async Task<object> ExecuteQuery(string connectionString, string commandString, IDictionary<string, object> parameters)
    {
        List<object> rows = new List<object>();

        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            using (SqlCommand command = new SqlCommand(commandString, connection))
            {
                this.AddParamaters(command, parameters);
                await connection.OpenAsync();
                using (SqlDataReader reader = await command.ExecuteReaderAsync(CommandBehavior.CloseConnection))
                {
                    object[] fieldNames = new object[reader.FieldCount];
                    for (int i = 0; i < reader.FieldCount; i++)
                    {
                        fieldNames[i] = reader.GetName(i);
                    }
                    rows.Add(fieldNames);

                    IDataRecord record = (IDataRecord)reader;
                    while (await reader.ReadAsync())
                    {
                        object[] resultRecord = new object[record.FieldCount];
                        record.GetValues(resultRecord);
                        for (int i = 0; i < record.FieldCount; i++)
                        {      
                            Type type = record.GetFieldType(i);
                            if (resultRecord[i] is System.DBNull)
                            {
                                resultRecord[i] = null;
                            }
                            else if (type == typeof(byte[]) || type == typeof(char[]))
                            {
                                resultRecord[i] = Convert.ToBase64String((byte[])resultRecord[i]);
                            }
                            else if (type == typeof(Guid) || type == typeof(DateTime))
                            {
                                resultRecord[i] = resultRecord[i].ToString();
                            }
                            else if (type == typeof(IDataReader))
                            {
                                resultRecord[i] = "<IDataReader>";
                            }
                        }

                        rows.Add(resultRecord);
                    }
                }
            }
        }

        return rows;
    }
开发者ID:ryan-allen,项目名称:edge-sql,代码行数:53,代码来源:EdgeCompiler.cs

示例9: GetConnecton

    public static async Task<SqlConnection> GetConnecton(string transportAddress)
    {
        string connectionString = transportAddress.StartsWith("Samples.SqlServer.StoreAndForwardSender") || transportAddress == "error"
            ? SenderConnectionString 
            : ReceiverConnectionString;

        SqlConnection connection = new SqlConnection(connectionString);

        await connection.OpenAsync();

        return connection;
    }
开发者ID:odelljl,项目名称:docs.particular.net,代码行数:12,代码来源:ConnectionProvider.cs

示例10: ExecuteCommandWithNewConnectionAsync

        private static async Task ExecuteCommandWithNewConnectionAsync(string processName, string cmdText, ICollection<string> executedProcessList)
        {
            var conn = new SqlConnection(DataTestUtility.TcpConnStr);

            await conn.OpenAsync();
            var cmd = new SqlCommand(cmdText, conn);

            using (SqlDataReader reader = await cmd.ExecuteReaderAsync())
            {
                while (await reader.ReadAsync())
                {
                    executedProcessList.Add(processName);
                }
            }
        }
开发者ID:ChuangYang,项目名称:corefx,代码行数:15,代码来源:DDAsyncTest.cs

示例11: ExecuteQuery

    async Task<object> ExecuteQuery(IDictionary<string, object> parameters, SqlCommand command, SqlConnection connection)
    {
        List<object> rows = new List<object>();
        this.AddParamaters(command, parameters);
        await connection.OpenAsync();
        using (SqlDataReader reader = await command.ExecuteReaderAsync(CommandBehavior.CloseConnection))
        {
            IDataRecord record = (IDataRecord)reader;
            while (await reader.ReadAsync())
            {
                var dataObject = new ExpandoObject() as IDictionary<string, Object>;
                var resultRecord = new object[record.FieldCount];
                record.GetValues(resultRecord);

                for (int i = 0; i < record.FieldCount; i++)
                {      
                    Type type = record.GetFieldType(i);
                    if (resultRecord[i] is System.DBNull)
                    {
                        resultRecord[i] = null;
                    }
                    else if (type == typeof(byte[]) || type == typeof(char[]))
                    {
                        resultRecord[i] = Convert.ToBase64String((byte[])resultRecord[i]);
                    }
                    else if (type == typeof(Guid) || type == typeof(DateTime))
                    {
                        resultRecord[i] = resultRecord[i].ToString();
                    }
                    else if (type == typeof(IDataReader))
                    {
                        resultRecord[i] = "<IDataReader>";
                    }

                    dataObject.Add(record.GetName(i), resultRecord[i]);
                }

                rows.Add(dataObject);
            }

            return rows;
        }
    }
开发者ID:edwincrug,项目名称:flotilla,代码行数:43,代码来源:EdgeCompiler.cs

示例12: DeleteQueuesForEndpoint

    public static async Task DeleteQueuesForEndpoint()
    {
        var connectionString = @"Data Source=.\SQLEXPRESS;Initial Catalog=samples;Integrated Security=True";

        #region sqlserver-delete-queues-endpoint-usage

        using (var sqlConnection = new SqlConnection(connectionString))
        {
            await sqlConnection.OpenAsync()
                .ConfigureAwait(false);
            await DeleteQueuesForEndpoint(
                connection: sqlConnection,
                schema: "dbo",
                endpointName: "myendpoint")
                .ConfigureAwait(false);
        }

        #endregion
    }
开发者ID:chriscatilo,项目名称:docs.particular.net,代码行数:19,代码来源:QueueDeletion.cs

示例13: OtherEndpointConnectionParamsPull

    void OtherEndpointConnectionParamsPull(EndpointConfiguration endpointConfiguration)
    {
#pragma warning disable 0618

        #region sqlserver-multidb-other-endpoint-connection-pull

        var transport = endpointConfiguration.UseTransport<SqlServerTransport>();
        transport.EnableLegacyMultiInstanceMode(async address =>
        {
            var connectionString = address.Equals("RemoteEndpoint") ? "SomeConnectionString" : "SomeOtherConnectionString";
            var connection = new SqlConnection(connectionString);
            await connection.OpenAsync()
            .ConfigureAwait(false);
            return connection;
        });

        #endregion

#pragma warning restore 0618
    }
开发者ID:chriscatilo,项目名称:docs.particular.net,代码行数:20,代码来源:MultiDb.cs

示例14: ConnectionFactory

    void ConnectionFactory(EndpointConfiguration endpointConfiguration)
    {
        #region sqlserver-custom-connection-factory

        var transport = endpointConfiguration.UseTransport<SqlServerTransport>();
        transport.UseCustomSqlConnectionFactory(async () =>
        {
            connectionString = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;

            var connection = new SqlConnection(connectionString);
            await connection.OpenAsync()
                .ConfigureAwait(false);

            // perform custom operations

            return connection;
        });

        #endregion
    }
开发者ID:chriscatilo,项目名称:docs.particular.net,代码行数:20,代码来源:NamedConnectionString.cs

示例15: AsyncMain

    static async Task AsyncMain()
    {
        Console.Title = "Samples.SqlServer.StoreAndForwardReceiver";
        EndpointConfiguration endpointConfiguration = new EndpointConfiguration("Samples.SqlServer.StoreAndForwardReceiver");
        endpointConfiguration.SendFailedMessagesTo("error");

        #region ReceiverConfiguration

        endpointConfiguration.UseTransport<SqlServerTransport>()
            .EnableLagacyMultiInstanceMode(async transportAddress =>
            {
                string connectionString = transportAddress.StartsWith("Samples.SqlServer.StoreAndForwardReceiver") || transportAddress == "error"
                    ? ReceiverConnectionString
                    : SenderConnectionString;

                SqlConnection connection = new SqlConnection(connectionString);
                await connection.OpenAsync();
                return connection;
            });


        #endregion

        endpointConfiguration.UsePersistence<InMemoryPersistence>();
        endpointConfiguration.DisableFeature<SecondLevelRetries>();

        IEndpointInstance endpoint = await Endpoint.Start(endpointConfiguration);
        try
        {
            Console.WriteLine("Press any key to exit");
            Console.WriteLine("Waiting for Order messages from the Sender");
            Console.ReadKey();
        }
        finally
        {
            await endpoint.Stop();
        }
    }
开发者ID:odelljl,项目名称:docs.particular.net,代码行数:38,代码来源:Program.cs


注:本文中的SqlConnection.OpenAsync方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。