chhyong88

添加数据库csdn

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data.SqlClient;
namespace importCSDN
{
    class Program
    {
        static void Main(string[] args)
        {
            string constr = "Data Source=.\\SQLEXPRESS;Initial Catalog=db_userpsw;Integrated Security=True";
            SqlHelper sqlHelper = new SqlHelper(constr);

            StreamReader objReader = new StreamReader("c:\\csdn.txt");
            string sLine = "";
            string sqlstr = "";
            int i = 0;

            string name= "";
            string psw = "";
            string email = "";

            while (sLine != null)
            {
                sLine = objReader.ReadLine();
                if (sLine != null)
                {
                    string[] strArr = sLine.Split(\'#\');
                    if (strArr.Length == 2)
                        email = "";
                    if (strArr.Length == 1)
                    { email = ""; psw = ""; }
                    if (strArr.Length == 3)
                    {
                        name = strArr[0].Trim();
                        psw = strArr[1].Trim();
                        email = strArr[2].Trim();
                    }

                    SqlParameter paramNmae = new SqlParameter("@name",System.Data.SqlDbType.NVarChar,50);
                    paramNmae.Value = name;
                    SqlParameter paramPsw = new SqlParameter("@psw",System.Data.SqlDbType.NVarChar,50);
                    paramPsw.Value = psw;
                    SqlParameter paramEmail = new SqlParameter("@email",System.Data.SqlDbType.NVarChar,50);
                    paramEmail.Value = email;
                    
                    SqlParameter[] sqlparams = {paramNmae,paramPsw,paramEmail};

                    sqlstr = "insert into dbo.csdn (name,psw,email) values (@name,@psw,@email)";
                    sqlHelper.ExecuteNonQuery(sqlstr,sqlparams);
                    i++;
                    if(i%1000==0)
                        Console.WriteLine(i);

                }
            }
 
        }
    }
}

  

分类:

技术点:

相关文章: