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



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

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