Connexion to database SQL SERVER (2019)

sblb 1,171 Reputation points
2022-04-22T15:01:09.27+00:00

Hi,
I've an Blazor Wasm application.
Until now I worked with a local database that I've created by migration.

"Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=C:\\Users\\...\\source\\repos\\DATASERVEUR\\Developers.mdf;Integrated Security=True;Connect Timeout=30"

With this connection all things is ok

Now I want to connect my application with the data base in sql server (2019)

Data Source=servername;Initial Catalog=Developers;Integrated Security=True;

the table dbo.Developers was created by import of the flat file (csv)

When I run my application I've a message :

InvalidCastException: Unable to cast object of type 'System.Byte' to type 'System.Int32'.

The get action where I've this message :

  [HttpGet]
        public async Task<IActionResult> Get()
        {           
            var devs = await _context.Developers.ToListAsync();
            return Ok(devs);
        }

Have you an idea how I can solve this problem?
thanks in advance

Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,541 questions
SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,584 questions
SQL Server Migration Assistant
SQL Server Migration Assistant
A Microsoft tool designed to automate database migration to SQL Server from Access, DB2, MySQL, Oracle, and SAP ASE.
524 questions
{count} votes

8 answers

Sort by: Most helpful
  1. Erland Sommarskog 109.3K Reputation points MVP
    2022-04-23T18:03:42.907+00:00

    I'm not very good at using the Import Wizard. However, I was able to load your sample file with BULK INSERT:

    BULK INSERT Developers FROM 'F:\FTP\temptab.csv'
    WITH (FIELDTERMINATOR = ';', FIRSTROW = 2)
    

    This requires that SQL Server can access the file. If that is an issue, you can use BCP from the command line:

    bcp tempdb.dbo.Developers in F:\FTP\temptab.csv -c -t; -S YOURSERVER  -T F 2
    

    Although to load the file through BCP, I had to change the columns AmdtC, AmdtP and IndiceP to permit NULL.

    0 comments No comments

  2. sblb 1,171 Reputation points
    2022-04-25T15:16:17.197+00:00

    Thanks to your reply.
    I had also tried the sql code with bulk insert but I had an error.
    I think you have modified the datetime columns. because they are declared as "not null". So the colonm DAteSoldePr is null so you must have made a mistake

    To make the column "not null" we can do this as follows:

    public DateTime? DateCrea { get; set; }
    

    But after I have a problem with the variable calcul e.g if (DateSoldePr.Year < 2000) because .Year is not defined for DateSoldePr = null

    In my code I call the variable DateSoldePr and if I add ? it still brings a problem

    Have you an idea?


  3. sblb 1,171 Reputation points
    2022-04-26T05:22:29.79+00:00

    Right DateSildePr is with default value in class.
    After migration DateSoldePr is not null.

    Hep Guy, I never wrote that you have made a mistake. I only wrote that you have to modify my data to do bulk insert.

    Thanks Sir


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.