A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© Always. 中级黑马   /  2013-9-22 17:31  /  1537 人查看  /  10 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 Always. 于 2013-9-22 22:58 编辑

我实现了将数据从一个txt文件中导入到VS2010中创建的数据库中。
代码如下:
  1. if(ofdImport.ShowDialog()!=DialogResult.OK)
  2.             {
  3.                 MessageBox.Show("导入失败");
  4.             }     
  5.   string dataDir = AppDomain.CurrentDomain.BaseDirectory;
  6.             if (dataDir.EndsWith(@"\bin\Debug\")
  7.                 || dataDir.EndsWith(@"\bin\Release\"))
  8.             {
  9.                 dataDir = System.IO.Directory.GetParent(dataDir).Parent.Parent.FullName;
  10.                 AppDomain.CurrentDomain.SetData("DataDirectory", dataDir);
  11.             }
  12.             using (SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=E:\我的第一个mdf解决\数据导入导出\Database4.mdf;Integrated Security=True;User Instance=True"))
  13.             {
  14.                 conn.Open();
  15. using (SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=E:\我的第一个mdf解决\数据导入导出\Database4.mdf;Integrated Security=True;User Instance=True"))
  16.             {
  17.                 conn.Open();
  18.                 using (SqlCommand cmd = conn.CreateCommand())
  19.                 {
  20.                     cmd.CommandText = "insert into T_Persons  values(@name,@age)";
  21.                     using (FileStream filestream = File.OpenRead(ofdImport.FileName))                   //打开这个文件
  22.                     {
  23.                     using (StreamReader streamreader = new StreamReader(filestream))               //读取导入的这个文件
  24.                     {
  25.                         string line = null;
  26. while ((line = streamreader.ReadLine()) != null)
  27.                         {
  28.                             string[] remove = line.Split('|');
  29.                             string name = remove[0];
  30.                             int age = Convert.ToInt32(remove[1]);
  31.                             cmd.Parameters.Clear();
  32.                             cmd.Parameters.Add(new SqlParameter("name", name));
  33.                             cmd.Parameters.Add(new SqlParameter("@age", age));
  34.                             cmd.ExecuteNonQuery();
  35.                         }
  36.                     }
  37.                 }
  38.                 }
  39.             }
复制代码
VS2010中创建的数据库 中导出数据到txt中
代码如下:
  1.         if (ofdOutput.ShowDialog() != DialogResult.OK)
  2.             {
  3.                 MessageBox.Show("导出失败");
  4.             }
  5.             string dataDir = AppDomain.CurrentDomain.BaseDirectory;
  6.             if (dataDir.EndsWith(@"\bin\Debug\")
  7.                 || dataDir.EndsWith(@"\bin\Release\"))
  8.             {
  9.                 dataDir = System.IO.Directory.GetParent(dataDir).Parent.Parent.FullName;
  10.                 AppDomain.CurrentDomain.SetData("DataDirectory", dataDir);
  11.             }
  12.             using (SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=E:\我的第一个mdf解决\数据导出\Database6.mdf;Integrated Security=True;User Instance=True"))
  13.             {
  14.                 conn.Open();
  15.                 using (SqlCommand cmd = conn.CreateCommand())
  16.                 {
  17.                     cmd.CommandText = "select * from T_Persons";
  18.                     using (SqlDataReader reader = cmd.ExecuteReader())
  19.                     {
  20.                             using (FileStream filestream = new FileStream(ofdOutput.FileName, FileMode.Append, FileAccess.Write))
  21.                             {
  22.                                 StreamWriter sw = new StreamWriter(filestream);
  23.    whlile (reader.Read())                   //逐行读取每一个信息,比如第一行
  24.    {
  25.    string name = reader.GetString(reader.GetOrdinal("Name"));   //比如读取第一行中Name那里列中的内容
  26.   string age = reader.GetString(reader.GetOrdinal("Age"));      /比如读取第一行中Age那里列中的内容   
  27.      string line = name + "|" + age;
  28.                                     sw.WriteLine(line);
  29.                                 }
  30.                                 sw.Flush();  
  31.                     }
  32.                 }
  33.                     MessageBox.Show("导出成功");
  34.             }
  35.         
  36.         
复制代码

评分

参与人数 1技术分 +1 收起 理由
haxyek + 1

查看全部评分

10 个回复

倒序浏览
妹子,你的代码不全啊,怎么能知道你的问题出在哪呢?
回复 使用道具 举报
七里香 发表于 2013-9-22 18:38
妹子,你的代码不全啊,怎么能知道你的问题出在哪呢?

我首先粘贴的时候,代码是全的。不知道为什么提交之后就成这个样子了。
回复 使用道具 举报
七里香 发表于 2013-9-22 18:38
妹子,你的代码不全啊,怎么能知道你的问题出在哪呢?

学长,谢谢。我刚知道代码突然不全了,本来发帖的时候还检查了一遍都没有错误。非常感谢及时的提醒,不然我还一直死死等着回复呢!已经把代码重新传了一次。
回复 使用道具 举报
问题一:导入数据有几条?
回复 使用道具 举报
你上面的代码应该不会只输出一行的,要么就是你的数据表里面只有一条数据。但是你这个代码运行的话会有问题。下面是我把我以前的代码改得和你的差不多的一段代码,你可以参考一下:
  1. using (SqlConnection conn = new SqlConnection(@"Data Source = .; Initial Catalog = DataBase1; User ID = sa; Pwd = 1234;"))
  2.             {
  3.                 conn.Open();
  4.                 using (SqlCommand comm = conn.CreateCommand())
  5.                 {
  6.                     comm.CommandText = "select Name,Age from T_Persons ";
  7.                     using (SqlDataReader reader = comm.ExecuteReader())
  8.                     {
  9.                         using (FileStream fileStream = new FileStream(sfdExport.FileName,FileMode.Append,FileAccess.Write))
  10.                         {
  11.                             using (StreamWriter streamWriter = new StreamWriter(fileStream))
  12.                             {
  13.                                 while (reader.Read())
  14.                                 {
  15.                                     //string name = reader["Name"].ToString();
  16.                                     //string age = reader["age"].ToString();
  17.                                     //string str = string.Join("|", new string[] { name, age });
  18.                                     string name = reader.GetString(reader.GetOrdinal("Name"));
  19.                                     //不能用这种方法获取Age的值,因为它是Int32类型的
  20.                                     //string age = reader.GetString(reader.GetOrdinal("age"));
  21.                                     int age = reader.GetInt32(reader.GetOrdinal("age"));
  22.                                     string str = name + "|" + age;

  23.                                     streamWriter.WriteLine(str);
  24.                                 }
  25.                                 streamWriter.Flush();
  26.                             }
  27.                         }
  28.                     }
  29.                 }
  30.             }
复制代码
回复 使用道具 举报
本帖最后由 Always. 于 2013-9-22 23:17 编辑
leayon 发表于 2013-9-22 23:06
你上面的代码应该不会只输出一行的,要么就是你的数据表里面只有一条数据。但是你这个代码运行的话会有问题 ...

恩恩。谢谢。
这个代码是我改了之后的代码,已经可以运行了。
不过,    关于(//不能用这种方法获取Age的值,因为它是Int32类型的 )这个,这里是可以的,因为此处的age没有当做int类型来处理,只把它当做了字符串来处理。只需要有GetString即可。若是需要计算年龄的时候,就应该使用GetInt32.   你的意思应该是这样,对吗
回复 使用道具 举报
leayon 中级黑马 2013-9-23 09:50:53
8#
Always. 发表于 2013-9-22 23:14
恩恩。谢谢。
这个代码是我改了之后的代码,已经可以运行了。
不过,    关于(//不能用这种方法获取Age的 ...

那就有点奇怪了,我的程序运行到string age = reader.GetString(reader.GetOrdinal("age"));  这一行就会报错反而是用reader.GetInt32这种方法才可以运行。不解。。。
回复 使用道具 举报
leayon 发表于 2013-9-23 09:50
那就有点奇怪了,我的程序运行到string age = reader.GetString(reader.GetOrdinal("age"));  这一行就会 ...

{:soso_e113:}我的木有出错,你后边是不是使用了age这个变量 ?
回复 使用道具 举报
leayon 发表于 2013-9-23 09:50
那就有点奇怪了,我的程序运行到string age = reader.GetString(reader.GetOrdinal("age"));  这一行就会 ...

我知道为什么了。。你看我的定义表的时候是这样的:

你的应该是把Age的数据类型设为int了。。应该问题就是出现在这里

QQ图片20130923114926.jpg (27.28 KB, 下载次数: 52)

表的定义

表的定义
回复 使用道具 举报
Always. 发表于 2013-9-23 11:50
我知道为什么了。。你看我的定义表的时候是这样的:

你的应该是把Age的数据类型设为int了。。应该问题就 ...

原来如此,多谢{:soso_e181:}
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马