| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 688 人关注过本帖
标题:在导入文件是出现了错误,请解答
只看楼主 加入收藏
Dazzler
Rank: 2
等 级:论坛游民
帖 子:14
专家分:27
注 册:2011-11-15
结帖率:100%
收藏
已结贴  问题点数:10 回复次数:2 
在导入文件是出现了错误,请解答
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using
using System.Data.SqlClient;

namespace 数据的导入导出
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnImport_Click(object sender, EventArgs e)
        {
            if (ofdImport.ShowDialog() == DialogResult.OK)
            {
                using (FileStream fileStream = File.OpenRead(ofdImport.FileName))
                {
                    using (StreamReader streamReader = new StreamReader(fileStream))
                    {
                        string line = null;
                        while ((line = streamReader.ReadLine()) != null)
                        {
                            string[] strs = line.Split('|');
                            string name = strs[0];
                            int age = Convert.ToInt32(strs[1]);
                            using (SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDBFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True"))
                            {
                                conn.Open();
                                using (SqlCommand cmd = conn.CreateCommand())
                                {
                                     = "insert into T_Person1(Name,Age) values(@Name,@Age)";
                                    cmd.Parameters.Add(new SqlParameter("Name", strs[0]));
                                    cmd.Parameters.Add(new SqlParameter("Age", strs[1]));
                                    cmd.ExecuteNonQuery();
                                }
                            }
                        }
                        MessageBox.Show("添加成功");
                    }
                }
            }
        }
    }
}

提示错误是:在 System.Data.SqlClient.SqlException 中第一次偶然出现的“System.Data.dll”类型的异常
我建的数据库中有一个表 T_Person1(Id,Name,Age);
搜索更多相关主题的帖子: private public 
2011-11-29 06:57
jedypjd
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:蒙面侠
威 望:9
帖 子:1096
专家分:4969
注 册:2009-7-27
收藏
得分:0 
参数没Id?

天涯无岁月,歧路有风尘,百年浑似醉,是非一片云
2011-11-29 09:23
wangnannan
Rank: 18Rank: 18Rank: 18Rank: 18Rank: 18
等 级:贵宾
威 望:87
帖 子:2546
专家分:9359
注 册:2007-11-3
收藏
得分:10 
可以加try catch看看抛出异常是什么信息

出来混,谁不都要拼命的嘛。 。拼不赢?那就看谁倒霉了。 。有机会也要看谁下手快,快的就能赢,慢。 。狗屎你都抢不到。 。还说什么拼命?
2011-11-29 09:26
快速回复:在导入文件是出现了错误,请解答
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014779 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved