ADO.NET 学习笔记(八) 练习:数据的导入

static void Main(string[] args)

{

  //连接数据库

  string dataDir = AppDomain.CurrentDomain.BaseDirectory;

  if (dataDir.EndsWith(@"\bin\Debug\")||dataDir.EndsWith(@"\bin\Release\"))

  {

    dataDir = System.IO.Directory.GetParent(dataDir).Parent.Parent.FullName;

    AppDomain.CurrentDomain.SetData("DataDirectory", dataDir);

  }

}



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[] str = line.Split('|');

          string province = str[0];

          string city = str[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_Province (FProvince,FCity) values (@Province,@City)";

              cmd.Parameters.Add(new SqlParameter("Province", province));

              cmd.Parameters.Add(new SqlParameter("City", city));

              cmd.ExecuteNonQuery();

            }

          }

        }

      }

    }

  }

  MessageBox.Show("导入成功");

} 
while ()

{

  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())

    {



    }

  }

}

 

对于文件的每一行都执行创建连接,打开,执行,关闭这样的流程。连接数据库是非常耗时的一个过程,这样的代码效率无疑是低下的。(连接过程非常耗时)
解决方法:连接上数据库后执行读取、写入工作,完成后再关闭,这样只执行一次创建连接,打开,关闭的操作,甚至Command都无需每次创建,应当设法重用Command。

 

if (ofdImport.ShowDialog() != DialogResult.OK)

{

  return;

}

//文件流读写

using (FileStream fileStream = File.OpenRead(ofdImport.FileName))

{

  using (StreamReader streamReader = new StreamReader(fileStream, System.Text.Encoding.Default))

  {

    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_Province (FProvince,FCity) values (@Province,@City)";

        string line = null;

        //先打开连接以后再执行while

        while ((line = streamReader.ReadLine()) != null)

        {

          string[] str = line.Split('|');

          string province = str[0];

          string city = str[1];



          cmd.Parameters.Clear();//注意不能丢。

          cmd.Parameters.Add(new SqlParameter("Province", province));

          cmd.Parameters.Add(new SqlParameter("City", city));

          cmd.ExecuteNonQuery();

        }

      }

    }

  }

}

MessageBox.Show("导入成功");

你可能感兴趣的:(.net)