CSharp之ADO.NET操作SqlServer数据库数据导入导出
数据库文件导出 namespace _02数据库文件导出 { ? ? class Program ? ? { ? ? ? ? static void Main(string[] args) ? ? ? ? { ? ?//连接字符串 ? ? ? ? ? ? string str = "Data Source=.SQLExpress;Initial Catalog=Test;Integrated Security=True"; ? ? ? ? ? ? ? ? ? ? //创建连接对象 ? ? ? ? ? ? using (SqlConnection con=new SqlConnection(str)) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? //创建sql语句 ? ? ? ? ? ? ? ? string sql = "select UserId,UserName,UserPwd from UserLogin"; ? ? ? ? ? ? ? ? ? ? //创建命令对象,并传入sql语句和连接对象 ? ? ? ? ? ? ? ? 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},reader[0],reader[1],reader[2]); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ?}? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? ? ? Console.WriteLine("导出数据成功!"); ? ? ? ? ? ? Console.ReadKey(); ? ? ? ? } ? ? } } 导出后的数据: 2.导入文本文件数据到数据库: : 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",? ? ? ? ? ? ? ? ? ? ? }; ? ? ? ? ? ? ? ? ? ? using (SqlCommand cmd=new SqlCommand(sql,con)) ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? //AddRange(ps)要放在外边,放在while循环里面会又添加一次ps,会报错 ? ? ? ? ? ? ? ? ? ? ? ? cmd.Parameters.AddRange(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(); ? ? ? ? } ? ? } }
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |