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


  • 相关阅读:
    每天一个linux命令(57):ss命令
    [原][osg][osgearth]我眼中的osgearth
    [osg]osgDB的加载机制,使用3DS插件做参考(转,整理现有osgDB资料)
    [原][osgearth]osgearthviewer读取earth文件,代码解析(earth文件读取的一帧)
    [原][译][osgearth]Model Source Drivers模型驱动源(OE官方文档翻译)
    [原][译][osgearth]样式表style中参数总结(OE官方文档翻译)
    [原][osgearth]API加载earth文件的解析
    [原][译][osgearth]API加载地球(OE官方文档翻译)
    [osgearth]Earth文件详解
    [osgearth]通过API创建一个earth模型
  • 原文地址:https://www.cnblogs.com/CSharpLover/p/5193682.html
Copyright © 2020-2023  润新知