You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sqlserver.md 1.9 KiB

4 years ago
4 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # SQL Server
  2. SQL Server is a relational database management system developed by Microsoft. CAP supports SQL Server database.
  3. !!! warning "Warning"
  4. We currently use `Microsoft.Data.SqlClient` as the database driver, which is the future of SQL Server drivers, and we have abandoned `System.Data.SqlClient`, we suggest that you switch to.
  5. ## Configuration
  6. To use SQL Server storage, you need to install the following package from NuGet:
  7. ```powershell
  8. PM> Install-Package DotNetCore.CAP.SqlServer
  9. ```
  10. Next, add configuration items to the `ConfigureServices` method of `Startup.cs`.
  11. ```csharp
  12. public void ConfigureServices(IServiceCollection services)
  13. {
  14. // ...
  15. services.AddCap(x =>
  16. {
  17. x.UseSqlServer(opt=>{
  18. //SqlServerOptions
  19. });
  20. // x.UseXXX ...
  21. });
  22. }
  23. ```
  24. #### SqlServerOptions
  25. NAME | DESCRIPTION | TYPE | DEFAULT
  26. :---|:---|---|:---
  27. Schema | Database schema | string | Cap
  28. ConnectionString | Database connection string | string |
  29. ## Publish with transaction
  30. ### ADO.NET with transaction
  31. ```csharp
  32. private readonly ICapPublisher _capBus;
  33. using (var connection = new SqlConnection("ConnectionString"))
  34. {
  35. using (var transaction = connection.BeginTransaction(_capBus, autoCommit: false))
  36. {
  37. //your business code
  38. connection.Execute("insert into test(name) values('test')",
  39. transaction: (IDbTransaction)transaction.DbTransaction);
  40. _capBus.Publish("sample.rabbitmq.mysql", DateTime.Now);
  41. transaction.Commit();
  42. }
  43. }
  44. ```
  45. ### EntityFramework with transaction
  46. ```csharp
  47. private readonly ICapPublisher _capBus;
  48. using (var trans = dbContext.Database.BeginTransaction(_capBus, autoCommit: false))
  49. {
  50. dbContext.Persons.Add(new Person() { Name = "ef.transaction" });
  51. _capBus.Publish("sample.rabbitmq.mysql", DateTime.Now);
  52. dbContext.SaveChanges();
  53. trans.Commit();
  54. }
  55. ```