简体   繁体   中英

Issue with Dapper connection: TCP Provider, error: 40 - Could not open a connection to SQL Server

I am trying to use Dapper to execute quires within a Microsoft SQL Server. The code I am using is the following:

public void Insert_Cycle(int cylinderId, int cycleNumber, float cycleMaxPressure, float cyleMinPressure, DateTime cycleStartTime, DateTime cyleEndTime)
{
    using (IDbConnection db = new SqlConnection(ConfigurationManager.ConnectionStrings["DB Name"].ConnectionString))
    {
        Models.T6Testing newcycle = new Models.T6Testing { CylinderId = cylinderId, CycleNumber = cycleNumber, CycleMaxPressure = cycleMaxPressure, CyleMinPressure = cyleMinPressure, CycleStartTime = cycleStartTime, CyleEndTime = cyleEndTime };
        List<Models.T6Testing> cycles = new List<Models.T6Testing>();
        cycles.Add(new Models.T6Testing { CylinderId = cylinderId, CycleNumber = cycleNumber, CycleMaxPressure = cycleMaxPressure, CyleMinPressure = cyleMinPressure, CycleStartTime = cycleStartTime, CyleEndTime = cyleEndTime });
        db.Execute("dbo.SP_INSERT_CYCLE @Cylinder_ID,@Cycle_number,@Cycle_max_pressure,@Cycle_min_pressure,@Cycle_start_time,@Cycle_end_time", cycles);
    }
}

The connection string is in the following format:

<connectionStrings>  
    <add name="DB Name"
         connectionString="Data Source=x.x.x.x,#port;Initial Catalog=Database Name;Persist Security Info=True;User ID=sa;Password=xxxxxx"
         providerName="System.Data.SqlClient" />
</connectionStrings>  

In order to test if the Connection String is in the right format I have used vttestconnectionstring from https://www.vadimtabakman.com/connection-string-tester.aspx , and I was able to run queries with it. Could someone point me in the right direction where I am making a mistake?

The syntax and the connection string was correct. The issue was with the UWP project, where Internet was not added to capabilities in Package.appxmanifest

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM