MySql.Data.MySqlClient.MySqlBulkLoader.Load C# (CSharp) Method

Load() public method

Execute the load operation
public Load ( ) : int
return int
    public int Load()
    {
      bool openedConnection = false;

      if (Connection == null)
        throw new InvalidOperationException(Resources.ConnectionNotSet);

      // next we open up the connetion if it is not already open
      if (connection.State != ConnectionState.Open)
      {
        openedConnection = true;
        connection.Open();
      }

      try
      {
        string sql = BuildSqlCommand();
        MySqlCommand cmd = new MySqlCommand(sql, Connection);
        cmd.CommandTimeout = Timeout;
        return cmd.ExecuteNonQuery();
      }
      finally
      {
        if (openedConnection)
          connection.Close();
      }
    }

Usage Example

コード例 #1
1
ファイル: BulkLoading.cs プロジェクト: tdhieu/openvss
        public void BulkLoadEscaping()
        {
            execSQL("DROP TABLE IF EXISTS Test");
            execSQL("CREATE TABLE Test (id INT NOT NULL, name VARCHAR(250), name2 VARCHAR(250), PRIMARY KEY(id))");

            // first create the external file
            string path = Path.GetTempFileName();
            StreamWriter sw = new StreamWriter(path);
            for (int i = 0; i < 200; i++)
                sw.WriteLine(i + ",col1\tstill col1,col2");
            sw.Flush();
            sw.Close();

            MySqlBulkLoader loader = new MySqlBulkLoader(conn);
            loader.TableName = "Test";
            loader.FileName = path;
            loader.Timeout = 0;
            loader.EscapeCharacter = '\t';
            loader.FieldTerminator = ",";
            int count = loader.Load();
            Assert.AreEqual(200, count);

            MySqlDataAdapter da = new MySqlDataAdapter("SELECT * FROM Test", conn);
            DataTable dt = new DataTable();
            da.Fill(dt);
            Assert.AreEqual(200, dt.Rows.Count);
            Assert.AreEqual("col1still col1", dt.Rows[0][1]);
            Assert.AreEqual("col2", dt.Rows[0][2].ToString().Trim());
        }
All Usage Examples Of MySql.Data.MySqlClient.MySqlBulkLoader::Load