問題是出在轉到mysql的bit(1)後都變成true
無論csv檔案是填入0還是False,最終MySqlBulkLoader.Load()後查看資料庫都變成true
後來有查到說csv檔案那欄位不填任何值,之後塞到mysql的bit(1)後會成功變成false
但同事卻因為我改了csv檔匯出方式,在做其他處理事情的時候出問題
所以想問一下有沒有另一種解法
原本是想說看看能不能寫一個專屬的Load()
但沒有找到Load()的原始程式碼而作罷
感謝各位看到這邊
==================================================
底下是一部分的程式碼
流程大概是
1.建立連線
2.在mysql建立table
3.將DataTable匯出成csv
4.將csv讀出來塞進mysql
5.刪掉csv
public bool Bulk_To_TmpTable(DataTable dt, string TmpTableName, string TempTableCreate)
{
bool result = false;
MySqlCommand cmd = null;
MySqlBulkLoader copyLoader = null;
string tempCsvFileSpec = "";
string dicName = "_BulkTemp";
Guid tmpFileName;
StreamWriter sw = null;
try
{
tempCsvFileSpec = System.IO.Path.Combine(System.Environment.CurrentDirectory, dicName);
cmd = new MySqlCommand();
cmd.CommandTimeout = 0;
cmd.Connection = Connection;
if (dt.Rows.Count > 0)
{
//=============== Create a temp table. ===============
if (!string.IsNullOrEmpty(TempTableCreate))
{
cmd.CommandText = TempTableCreate;
cmd.ExecuteNonQuery();
}
//========= Use .csv to bulk insert into MySQL ===
CreateFolder(tempCsvFileSpec);
tmpFileName = Guid.NewGuid();
tempCsvFileSpec = System.IO.Path.Combine(tempCsvFileSpec, tmpFileName + ".csv");
sw = new StreamWriter(tempCsvFileSpec, false);//, Encoding.UTF8);
WriteDataTable(dt, sw, false);
sw.Close();
//========= Copy Datatable content to temp table===
copyLoader = new MySqlBulkLoader(Connection);
copyLoader.TableName = TmpTableName;
copyLoader.FileName = tempCsvFileSpec;
copyLoader.FieldTerminator = ",";
copyLoader.FieldQuotationCharacter = '"';
copyLoader.LineTerminator = Environment.NewLine;
copyLoader.Local = true;
int i = copyLoader.Load();
System.IO.File.Delete(tempCsvFileSpec);
result = true;
}
}
catch (Exception ex)
{
result = false;
}
finally
{
sw = null;
copyLoader = null;
cmd = null;
}
return result;
}
肉眼看到的 0,1 是「文字」或「數字」
不是 0x00, 0x01 的 bit 值
根據官網的說法如下
BIT values cannot be loaded directly using binary notation (for example, b'011010'). To work around this, use the SET clause to strip off the leading b' and trailing ' and perform a base-2 to base-10 conversion so that MySQL loads the values into the BIT column properly:
這篇寫的更好
你有兩個選擇
1.把 bit 欄位改成 tinyint
2.不用 MySqlBulkLoader,自己寫 SQL command 慢慢塞