Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
The code is executed and I do not receive any error but it does not show any information in the database table. I want send data from my csv file to my database

What I have tried:

C#
using System;
using System.IO;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Configuration;
using System.Data.SqlClient;
using Microsoft.Data.SqlClient;

namespace DataBase
{
    public class SeedData
    {
        static void Main(string[] args)
        {
            var lineNumber = 0;
            using (SqlConnection conn = new SqlConnection(@"Server=.\SQLEXPRESS; Integrated Security=True"))
            {

                conn.Open();
                using (StreamReader reader = new StreamReader(@"..\..\..\DBData\IMDBMovieData.csv"))
                {
                    while (!reader.EndOfStream)
                    {
                        var line = reader.ReadLine();
                        if (lineNumber != 0)
                        {
                            var values = line.Split(',');

                            var sql = "INSERT INTO RentalMoviesDatabase.dbo.Movie VALUES ('" + values[0] + "','" + values[1] + "','" + values[2] + "','" + values[3] + "', '" + values[4] + "','" + values[5] + "','" + values[6] + "','" + values[7] + "')";

                            var cmd = new SqlCommand();
                            cmd.CommandText = sql;
                            cmd.CommandType = CommandType.Text;
                            cmd.Connection = conn;
                            cmd.ExecuteNonQuery();
                        }
                       
                         
                    }

                    lineNumber++;
                   
                }
                conn.Close();

            }

            Console.WriteLine("Done!");
        }
    }
   
}
Posted
Updated 23-Nov-20 7:04am
v2
Comments
Richard MacCutchan 23-Nov-20 12:18pm    
Have you checked that your splitting of the string returns the correct values, and number of variables? Have you used the debugger to check that your SQL statement succeeds?

And, please, do not post success type messages ("Done!") unless you are actually checking that it is done.
Richard MacCutchan 23-Nov-20 12:47pm    
I have a better idea, you do the debugging and tell us the result.
PIEBALDconsult 23-Nov-20 12:24pm    
Maybe your system is saving you from yourself?

1 solution

NEVER, NEVER, NEVER use string concatenation to build an SQL query. You open yourself up to SQL Injection attacks. Google for "SQL Injection Attack" to find out why what you did is so bad.

This is bad:
C#
var sql = "INSERT INTO RentalMoviesDatabase.dbo.Movie VALUES ('" + values[0] + "','" + values[1] + "','" + values[2] + "','" + values[3] + "', '" + values[4] + "','" + values[5] + "','" + values[6] + "','" + values[7] + "')";


ALWAYS use parameters to pass in your values. Google for "C# sql paramaterized queries".
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900