在导入文件是出现了错误,请解答
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);