Click here to Skip to main content
15,913,941 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How do i write this data into a SQL database....
this is SQL query...

SQL
SELECT TOP 1000 [id]
      ,[code]
      ,[buying]
      ,[selling]
  FROM [test].[dbo].[Currency]


This is the Code! i need to submit the [0] element to code column,[1] Element to buying column and [2] Element to Selling column.

C#
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;


namespace ConsoleApplication10
{
    class Program
    {
        static void Main(string[] args)
        {
            var reader = new StreamReader(File.OpenRead(@"c:\users\afshandc\documents\visual studio 2012\Projects\ConsoleApplication10\ConsoleApplication10\ex.csv"));
            List headers = new List();

            while (!reader.EndOfStream)
            {
                var line = reader.ReadLine();
                var values = line.Split(',');

                Console.WriteLine(values[0].ToString() + "," + values[1].ToString() + "," + values[2].ToString());
            }

            Console.ReadLine();

        }
        
    }
    
}
Posted

1 solution

Hello,

You can use SQLCommand[^] to do this. Below code snippet will give you an idea.
C#
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
 

namespace ConsoleApplication10
{
    class Program
    {
        static void Main(string[] args)
        {
            SqlConnection con;
            SqlCommand cmd;
 
            var reader = new StreamReader(File.OpenRead(@"c:\users\afshandc\documents\visual studio 2012\Projects\ConsoleApplication10\ConsoleApplication10\ex.csv"));
            List headers = new List();

            con = new SqlConnection(conString); // Please change the connection string as per your database
            cmd = new SqlCommand("INSERT INTO Currency(code, buying, selling) VALUES(@ccyCode, @buyRate, @sellRate)", con);
            cmd.Parameters.Add("@ccyCode", SqlDbType.VarChar);
            cmd.Parameters.Add("@buyRate", SqlDbType.Decimal);
            cmd.Parameters.Add("@sellRate", SqlDbType.Decimal);
            con.Open();

            while (!reader.EndOfStream)
            {
                var line = reader.ReadLine();
                var values = line.Split(',');
 
                Console.WriteLine(values[0].ToString() + "," + values[1].ToString() + "," + values[2].ToString());
                cmd.Parameters[0].value = values[0];
                cmd.Parameters[1].value = Convert.ToDecimal(values[1]);
                cmd.Parameters[2].value = Convert.ToDecimal(values[2]);
                cmd.ExecuteNonQuery();
            }
            Console.ReadLine();
            con.Close();
        }
    }
}

TODO :Add necessary error handling

Regards,
 
Share this answer
 
Comments
Hawkeye101 24-Jul-13 6:12am    
Thanks!

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



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