本帖最后由 李玮 于 2012-6-4 17:04 编辑
下面是代码,运行后,数据导入对话框弹出,但是查看数据并没用导入
我是建的数据库名: Shujvdaoru.mdf 表名:TableUser
被插数据:
tom|30
jerry|20
kucy|25
我的代码:
private void button1_Click(object sender, EventArgs e)
{
if (odfimport.ShowDialog() == DialogResult.OK)
{
using (FileStream fileStream = File.OpenRead(odfimport.FileName))
{
using (StreamReader reader = new StreamReader(fileStream))
{
string line = null;
while ((line = reader.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|\Shujvdaoru.mdf;Integrated Security=True;User Instance=True
"))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "insert into TableUser (Name,Age) values (@Name,@Age)";
cmd.Parameters.Add(new SqlParameter("Name", name));
cmd.Parameters.Add(new SqlParameter("Age", age));
cmd.ExecuteNonQuery();
}
}
}
}
}
MessageBox.Show("导入成功!");
}
}
|