将值插入数据库C#和CSV [英] Insert Values into a database C# and CSV

查看:69
本文介绍了将值插入数据库C#和CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将此数据写入SQL数据库....

这是SQL查询...



How do i write this data into a SQL database....
this is SQL query...

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





这是守则!我需要将[0]元素提交给代码列,[1]元素购买列和[2]元素销售列。





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.

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();

        }
        
    }
    
}

推荐答案

您好,



您可以使用 SQLCommand [ ^ ]要做这个。下面的代码片段会给你一个想法。

Hello,

You can use SQLCommand[^] to do this. Below code snippet will give you an idea.
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 添加必要的错误处理



问候,


TODO :Add necessary error handling

Regards,


这篇关于将值插入数据库C#和CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆