Ado.Net小练习01(数据库文件导出,导入)

数据库文件导出主要程序:

<span style="font-family: Arial, Helvetica, sans-serif;"><span style="font-size:14px;">namespace _02数据库文件导出</span></span>

{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "Data Source=.\SQLExpress;Initial Catalog=Test;Integrated Security=True";


            using (SqlConnection con=new SqlConnection(str))
            {
                string sql = "select UserId, UserName, UserPwd from UserLogin";
                using (SqlCommand cmd=new SqlCommand(sql,con))
                {
                    con.Open();
                    using (SqlDataReader reader=cmd.ExecuteReader())
                    {
                        if (reader.HasRows)
                        {
                            using (StreamWriter sw=new StreamWriter("1.txt"))
                            {
                                sw.WriteLine("{0},{1},{2}",reader.GetName(0),reader.GetName(1),reader.GetName(2));    //获取列的名称
                                while (reader.Read())
                                {
                                    sw.WriteLine("{0},{1},{2}",reader[0],reader[1],reader[2]);
                                }
                            }
                            
                        }
                    }
                }//endusing
            }//endusing

            Console.WriteLine("导出数据成功!");
            Console.ReadKey();
        }
    }
}


导入数据到数据库:

namespace _03导入数据库
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "Data Source=.\SQLExpress;Initial Catalog=Test;Integrated Security=True";
            using (StreamReader reader=new StreamReader("13.txt"))
            {
                string line = reader.ReadLine();   //第一行列名读完了,不要了
                using (SqlConnection con=new SqlConnection(str))
                {
                    con.Open();
                    string sql = "insert into UserLogin values(@UserName, @UserPwd)";
                    SqlParameter[] ps =
                    {
                                     //告诉数据库,我的参数中存的值要以varchar类型存到表中
                        new SqlParameter("@UserName",SqlDbType.VarChar), 
                        new SqlParameter("@UserPwd",SqlDbType.VarChar), 
                    };




                    using (SqlCommand cmd=new SqlCommand(sql,con))
                    {
                        cmd.Parameters.AddRange(ps);     //AddRange(ps)要放在外边,放在while循环里面会又添加一次ps,会报错
                        while ((line = reader.ReadLine()) !=null)
                        {
                            string[] txts = line.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                                 //把参数用什么值替换
                            ps[0].Value = txts[1];//名字
                            ps[1].Value = txts[2];//密码
                            cmd.ExecuteNonQuery();
                        }
                    }
                }
            }
            Console.WriteLine("导入数据成功");
            Console.ReadKey();
        }
    }
}


原文地址:https://www.cnblogs.com/CSharpLover/p/5193682.html