小编ILo*_*low的帖子

处理数据库连接问题的正确方法

我在尝试连接数据库时遇到以下错误:

建立与SQL Server的连接时发生与网络相关或特定于实例的错误.服务器未找到或无法访问.验证实例名称是否正确,以及SQL Server是否配置为允许远程连接.(提供者:命名管道提供程序,错误:40 - 无法打开与SQL Server的连接)

现在有时我得到这个错误,有时我不这样做,例如:当我第一次运行我的程序时,它成功打开连接,当我第二次运行时,我得到这个错误,下次我再次运行我的程序然后我不会得到错误.

当我尝试通过SSMS连接到同一个数据库服务器,然后我能够成功连接,但我只在我的程序中遇到此网络问题.

数据库不在我的LOCAL中.它在AZURE上.

我的本地数据库没有出现此错误.

代码:

public class AddOperation
{
    public void Start()
    {
          using (var processor = new MyProcessor())
          {
              for (int i = 0; i < 2; i++)
              {
                  if(i==0)
                  {
                     var connection = new SqlConnection("Connection string 1");
                     processor.Process(connection);
                  }
                  else
                  {
                      var connection = new SqlConnection("Connection string 2");
                      processor.Process(connection);
                  }   
              }
          }
    }       
}

public class MyProcessor : IDisposable
{
    public void Process(DbConnection cn)
        {
            using (var …
Run Code Online (Sandbox Code Playgroud)

c# ado.net sqlconnection azure-sql-database

13
推荐指数
3
解决办法
1974
查看次数

如何利用泛型来填充派生类模型以避免代码重复?

我有两种类型,每种类型都有不同的处理逻辑.根据该处理,我正在准备一个结果并将其返回给消费者(mvc应用程序,控制台应用程序等..)

  • 输入1
  • 类型2

现在问题是一些代码在两种类型中都很常见.唯一不同的部分是Type1Manager,Type2Manager两个类型的class(),它们实际上包含处理type1 and type2和准备结果的逻辑(Type1Model,Type2Model).

public class Variant
{
    public int Id { get; set; }
    public string Name { get; set; }
    public List<Subvariants> Subvariants { get; set; }
}

public class Subvariants
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public abstract class BaseManager
{
    //Other shared code
    public abstract ExecutionResult GetExecutionResult(Variant model);
}

public class ExecutionResult
{
    public string Name { get; …
Run Code Online (Sandbox Code Playgroud)

c# generics

12
推荐指数
1
解决办法
263
查看次数

比较内存中的2个无序记录集

下面是我的应用程序数据库表,其中包含存储在表中的SQL查询:QueryStorage

Id           Query           ConnectionString       Rdbms
1            select...        Data Source           Sql Server
2            select...        Data Source           Oracle
Run Code Online (Sandbox Code Playgroud)

上表中的SQL查询是通过Web服务更新的,我们不允许在查询之上更新,但我们可以在查询之外添加一些内容,如下所示:

存储在表中的查询: select id as LinkedColumn, Amount as CompareColumn from Source

来自我的c#app的调整查询:select Q.LinkedColumn, Q.CompareColumn from (stored sql query) as Q

我想比较下面的2个无序列表:

Id = 1(Sql server)QueryStorage表记录执行的查询如下所示:

select Id as LinkedColumn,CompareColumn from Source
Run Code Online (Sandbox Code Playgroud)

清单1:

LinkedColumn     CompareColumn
1                100
2                200
3                300
4                400
5                500
6                600
7                700
8                800
9                900
10               1000
Run Code Online (Sandbox Code Playgroud)

Id = 2(Oracle)QueryStorage …

c# sql linq algorithm ado.net

10
推荐指数
2
解决办法
860
查看次数

如何强制hangfire服务器在重新启动时删除该特定服务器的旧服务器数据?

我正在显示当前在我的页面上运行的hangfire服务器列表.

我在控制台应用程序中运行hangfire服务器,但问题是当我没有运行我的控制台应用程序仍然hangfire api返回hangfire服务器.

此外,当我跑我的控制台应用程序多次,我得到3-4迟发型服务器虽然我只有1迟发型服务器控制台应用程序运行.

Mvc申请:

IMonitoringApi monitoringApi = JobStorage.Current.GetMonitoringApi();
var servers = monitoringApi.Servers().OrderByDescending(s => s.StartedAt);
Run Code Online (Sandbox Code Playgroud)

控制台应用程序:Hangfire服务器

public static void Main(string[] args)
{
    var sqlServerPolling = new SqlServerStorageOptions
    {
        QueuePollInterval = TimeSpan.FromSeconds(20) // Default value
    };
    GlobalConfiguration.Configuration.UseSqlServerStorage("ConnectionString", sqlServerPolling);

    // Set automatic retry attempt
    GlobalJobFilters.Filters.Add(new AutomaticRetryAttribute { Attempts = 0 });

    // Set worker count
    var options = new BackgroundJobServerOptions
    {
        WorkerCount = 1,
    };
    using (var server = new BackgroundJobServer(options))
    {
        Console.WriteLine("Hangfire Server1 started. Press any key to exit...");
        Console.ReadKey();
    } …
Run Code Online (Sandbox Code Playgroud)

c# hangfire hangfire-console

10
推荐指数
1
解决办法
1646
查看次数

在Ado.net的情况下,DataAccess层返回域对象?

我正在创建一个项目,我将使用ADO.NET作为数据访问层与数据库进行交互.

现在,我非常困惑的是:

1)我应该在这个应用程序中有域对象吗?

2)我的sql查询结果是否应始终与域对象绑定?

3)如果我不使用域对象,那么我是否总是从数据访问层返回自定义模型,其中包含我想要在web api服务中返回的所有内容?

4)如果我使用域模型,并且如果我想要显示来自多个表或这样的场景的数据,例如:

public class Employee
   {
      int id;
      List<Skills>();
   }
Run Code Online (Sandbox Code Playgroud)

我可以轻松地在EF中执行此操作,但是使用ado.net和域对象,其结构如下所示我将如何实现此目的:

public class Employee
   {
       int id;
   }

   public class Skill
   {
       int id;
       int EmployeeId;
   }
Run Code Online (Sandbox Code Playgroud)

Ofcouse我可以先获得员工名单,然后对于每个员工,我可以根据员工ID获得技能列表,但是这将是痛苦的,我将不得不为每个员工解雇查询以获得相应的技能,这在EF中非常简单基于导航属性并避免如下所示的开销:

var employees = //get listof employee in this case domain model 
                  List<Employee>
var employeeModel = new List<EmployeeModel>();
foreach(var employee in employees)
{
   EmployeeModel model = new EmployeeModel();
   model.id = employee.id;
   var skills = GetSkill(employee.id);//Get list of skills in this case 
                domain …
Run Code Online (Sandbox Code Playgroud)

c# ado.net repository-pattern asp.net-web-api

9
推荐指数
2
解决办法
495
查看次数

如何在类后面隐藏逻辑以提高方法的可读性并重构类以遵循SRP?

我有一种算法可以为实体创建一个版本,然后将其保存在下面的2个实体中:

1)变体

2)类别

interface IEntityVersion
{
    string GetVersion();
}

public class EntityVersion : IEntityVersion
{
    public string GetVersion()
    {
        return null;
    }
}

public interface IVariant
{
    void Process(Variant model, string connectionString);
}

public abstract class BaseVariant : IVariant
{
    private readonly IEntityVersion _entityVersion = new EntityVersion();

    public void Process(Variant model, string connectionString)
    {
        try
        {
            Transform();
            string variantVersion = _entityVersion.GetVersion();
            using (var myConnection = new SqlConnection(connectionString))
            {
                myConnection.Open();
                using (var transaction = myConnection.BeginTransaction())
                {
                    try
                    {
                        VariantRepo.UpdateVariantVersion(
                          myConnection,
                          transaction, …
Run Code Online (Sandbox Code Playgroud)

.net c# refactoring single-responsibility-principle

9
推荐指数
1
解决办法
382
查看次数

如何在单独的类库中管理客户端上下文对象?

我正在尝试为sharepoint 创建一个库(类库),它将使所有sharepoint dll与sharepoint服务器交互,以上传文件,文档并创建文档库和文档集.

现在这个库可以被Web应用程序(asp.net webform或mvc)或控制台应用程序或web api/wcf服务或windows形成任何东西的客户端使用.

所以在这里我对创建存储库模式和工作单元层以便管理客户端上下文对象感到困惑.

我不确定是否允许为每个操作创建客户端上下文,或者是否创建客户端上下文一次并重新使用或是否每次都创建clientcontext.

我有很多搜索,但无法找到任何参考或文章来创建存储库层,就像它创建的方式一样Entity framework DbContext.

我正在使用客户端对象模型(CSOM库),我的库是关于管理文件和元数据的内容管理系统.

我会感激任何帮助:)

更新:在sharepoint域上传文件的示例代码

ClientContext context = new ClientContext("http://SiteUrl"); // Starting with ClientContext, the constructor requires a URL to the server running SharePoint. 
context.Credentials = new SharePointOnlineCredentials(username, password);

// The SharePoint web at the URL.
Web myWeb = context.Web;

List myLibrary = myWeb.Lists.GetByTitle("MyProject"); //This is called document library so in sharepoint document Library is the root where …
Run Code Online (Sandbox Code Playgroud)

c# unit-of-work repository-pattern sharepoint-2013 csom

8
推荐指数
1
解决办法
831
查看次数

模型绑定不适用于嵌套对象

我正在尝试在我的以下模型中绑定邮递员发布的模型中的数据:

public class VariantModel
{
    public int Id { get; set; }     
    public List<SubvariantModel> Subvariants { get; set; }
}

public class SubvariantModel
{
    public int Id { get; set; }
    public string Description { get; set; }
    public IFormFile Document { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

每个属性都被填充,但只有Document属性变为空,如您所见:

在此处输入图片说明

但令人惊讶的是,当我检查 http 请求对象时,我看到了该文件:

在此处输入图片说明

这就是我从 POSTMAN 发布数据的方式: 在此处输入图片说明

在此处输入图片说明

代码 :

[HttpPost]
public void Post([FromForm]VariantModel emp)
{
    var d = HttpContext.Request;
}
Run Code Online (Sandbox Code Playgroud)

谁能告诉我这里可能有什么问题?

更新 :

在此处输入图片说明

c# asp.net-web-api postman asp.net-core

8
推荐指数
1
解决办法
1794
查看次数

即使在为操作设置超时属性后也会出现超时异常

超时问题:

超时已过期.操作完成之前经过的超时时间或服务器没有响应.\ r \n语句已终止.

我有17亿条记录要转储到我的应用程序数据库中.这1200万条记录是2条数据库记录之间比较操作的结果.

我比较了2个数据库记录,然后在数据表中填充不匹配记录(基于某些条件),一旦该数据表达到1000或500等限制,我将此数据表发送到SQL批量复制以进行批量导入,然后清空数据表.

我在事务中执行整个操作,以便我插入X记录,在比较过程中出现任何错误,所以我将回滚那些X记录.

但正因为如此,我得到一个超时问题然后批量复制.

我检查了各种不同的batchsize like 5000,1000,500,300等等.我在所有批量大小中都出现了超时问题.

一旦我将批量复制超时设置为0,然后我将此错误发送到下面:

我的数据库的事务日志已满.

有1000条记录,它达到270万,然后抛出超时问题,

有500条记录,它达到了大约210万条记录然后抛出错误.

300,200,100也是抛出超时错误.

我还在连接字符串中将连接超时设置为30分钟.

代码:

public class SaveRepo : IDisposable
    {
        DataTable dataTable;
        SqlConnection connection;
        string connectionString;
        SqlTransaction transaction;
        SqlBulkCopy bulkCopy;
        int testId,

        public SaveRepo (int testId)//testId=10364
        {
            this.connectionString = connectionString;
            dataTable = new DataTable();
            connection = new SqlConnection(connectionString);
            connection.Open();
            transaction = connection.BeginTransaction();
            bulkCopy = new SqlBulkCopy(connection, SqlBulkCopyOptions.Default, transaction);
            bulkCopy.BulkCopyTimeout = 60;
            bulkCopy.EnableStreaming = true;
            bulkCopy.DestinationTableName = "dbo.Sales";
            bulkCopy.BatchSize = 100;
            bulkCopy.SqlRowsCopied +=
                  new SqlRowsCopiedEventHandler(OnSqlRowsCopied); …
Run Code Online (Sandbox Code Playgroud)

c# sql-server ado.net sqlbulkcopy timeoutexception

7
推荐指数
1
解决办法
458
查看次数

在url中使用下划线连接复合键是好/坏设计吗?

我正在寻找以下用例的RESTful API设计的最佳实践:

Table1     Table2
Id1        Id1
Id2        Id2
Id3        Id3
Name       Name
           Table1Id1(FK to Table1)
           Table1Id1(FK to Table1)
           Table1Id1(FK to Table1)
Run Code Online (Sandbox Code Playgroud)

假设我有如下表1的端点:

/root/table1 (to get list of records)
/root/table2 (to get single record by primary key)
Run Code Online (Sandbox Code Playgroud)

现在我的问题是从第二个以下代表第二个网址中的复合键的最佳方式:

/root/e1/Id1/Id2/Id3

or 

/root/e1?Id1=1&Id2=2&Id3=3
Run Code Online (Sandbox Code Playgroud)

假设我有如下表2的端点:

/root/table1/Table1Id1_Table1Id2_Table1Id1/table2 (to get list of records for table2 by table1).
Run Code Online (Sandbox Code Playgroud)

现在这里是我的问题,在url之上是有效的,并且在复合键的情况下是否合适?

关于此用例的良好模式的任何建议将不胜感激.

api rest asp.net-web-api

6
推荐指数
2
解决办法
1637
查看次数