标题:在导入文件是出现了错误,请解答
取消只看楼主
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



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-356461-1-1.html




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

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