本帖最后由 宋天琪 于 2012-2-2 12:09 编辑
杨老师讲的数据导入导出,导入代码如下:
private void button1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
using (FileStream fileStream = File.OpenRead(openFileDialog1.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|\Database1.mdf;Integrated Security=True;User Instance=True
"))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "insert into T_Persons (Name,Age) values (@Name,@Age)";
cmd.Parameters.Add(new SqlParameter("Name", name));
cmd.Parameters.Add(new SqlParameter("Age", age));
cmd.ExecuteNonQuery();
}
}
}
}
}
MessageBox.Show("导入成功!");
}
}
导出的代码怎么写? |