C#读取文本文件中的内容,存入数据库

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using NLite.Data;

namespace ConsoleApplication1
{

    public class jiazhao
    {
        public string id { get; set; }
        public string question { get; set; }
        public string x1 { get; set; }
        public string x2 { get; set; }
        public string x3 { get; set; }
        public string x4 { get; set; }
        public string answer { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            //List<object> list = new List<object>();
            //list.Add(new { id = 0, name = "0" });
            //list.Add(new { id = 1, name = "1" });

            //foreach (var item in list)
            //{
            //    foreach (PropertyInfo property in item.GetType().GetProperties())
            //    {
            //        string objName = property.Name;
            //        if (objName == "id")
            //        {
            //            Console.WriteLine(property.GetValue(item, null));
            //        }
            //    }
            //}

            //DateTime dt1 = Convert.ToDateTime("2012-12-26 18:35:00");
            //DateTime dt2 = Convert.ToDateTime("2013-01-26 18:35:00");
            //Console.WriteLine((dt2-dt1).Days.ToString());
            //Console.WriteLine(DateTime.Compare(dt1, dt2));

            //StreamReader reader = new StreamReader(@"D:\Archive.txt");
            //string aLineData;
            //string[] TempArray;

            //while ((aLineData = reader.ReadLine()) != null)
            //{
            //    TempArray = aLineData.Split('\t');
            //    Console.WriteLine(TempArray.Length.ToString());
            //}

            int count = 0;
            int record = 0;
            string aLineData="";

            
            using (StreamReader reader = new StreamReader(@"D:\test.txt", Encoding.Default))
            {
                jiazhao zhao = new jiazhao();
                while ((aLineData = reader.ReadLine()) != null)
                {
                    
                    count++;
                    if (count == 1)
                    {
                        zhao.question = aLineData;
                    }
                    else if (count == 2)
                    {
                        zhao.x1 = aLineData;
                    }
                    else if (count == 3)
                    {
                        zhao.x2 = aLineData;
                    }
                    else if (count == 4)
                    {
                        zhao.x3 = aLineData;
                        
                    }
                    else if (count ==5)
                    {
                        zhao.x4 = aLineData;
                    }
                    else if (count == 6)
                    {
                        zhao.answer = aLineData;
                        record++;
                        zhao.id = string.Format("{0:D3}", record);
                        using (SqlConnection conn = new SqlConnection(@"server=localhost;database=DBTest;uid=sa;pwd=sa;"))
                        {
                            using (SqlCommand cmd = new SqlCommand())
                            {
                                cmd.Connection = conn;
                                cmd.CommandText = "insert into jiazhao values (@id,@question,@x1,@x2,@x3,@x4,@answer)";
                                cmd.Parameters.AddWithValue("@id", zhao.id);
                                cmd.Parameters.AddWithValue("@question", zhao.question);
                                cmd.Parameters.AddWithValue("@x1", zhao.x1);
                                cmd.Parameters.AddWithValue("@x2", zhao.x2);
                                cmd.Parameters.AddWithValue("@x3", zhao.x3);
                                cmd.Parameters.AddWithValue("@x4", zhao.x4);
                                cmd.Parameters.AddWithValue("@answer", zhao.answer);
                                cmd.Connection.Open();

                                count = cmd.ExecuteNonQuery();

                            }
                        }
                        Console.WriteLine(record+":"+zhao.question);
                        count = 0;
                    }


                    
                }
            }

            Console.ReadKey();

        }
    }
}

知识共享许可协议
《C#读取文本文件中的内容,存入数据库》常伟华 创作。
采用 知识共享 署名-相同方式共享 3.0 中国大陆 许可协议进行许可。
相邻依据:发表时间
  • 多说评论
  • 签名
  • 新浪微博
  • 默认评论
  • Tab Header 5

0 条评论 / 点击此处发表评论

Tab Content 5

开发技术


开发平台和工具

sitemap     155.08ms