using (FileStream fileStream = File.OpenRead(ofdImport.FileName))
{
using (StreamReader streamReader = new StreamReader(fileStream))
{
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())
{
string line = null;
cmd.CommandText = "insert into T_Persons(Name,Age) values(@Name,@Age)";
while ((line = streamReader.ReadLine()) != null)
{
string[] strs = line.Split('|');
string name = strs[0];
int age = Convert.ToInt32(strs[1]);
cmd.Parameters.Clear();
cmd.Parameters.Add(new SqlParameter("Name", name));
cmd.Parameters.Add(new SqlParameter("Age", age));
cmd.ExecuteNonQuery();
}
}
}
}
}
请以此为例, 解说一下每一步都有什么意义, 最好是声情并茂的解释,哈哈 |