ディレクトリのファイルを解析する/このmysqlデータベースのpostに基づいて挿入しようとしていますが、各列のcsv値を切り上げてデータベースのINT値になります。私はDECIMALを別のものに変更する必要があると思っていますが、私はそれを理解できませんでした。フォルダ内のcsvファイルを検索してmysqlデータベースに挿入
(例:1.11は、45.5は46になり、1になります)
csvファイル:
test1,test2,test3,test4
1.11,1.23,67.4,4.5
1.12,5.42,45.5,6.45
私のコード:
public void sqlTest()
{
string connectionString = "server=localhost;database=test;UserId=root;password=test;";
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
connection.Open();
using (MySqlCommand cmd = connection.CreateCommand())
{
cmd.CommandText = @"DROP TABLE test IF EXISTS";
cmd.CommandText = @"CREATE TABLE test (
Id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
test1 DECIMAL,
test2 DECIMAL,
test3 DECIMAL,
test4 DECIMAL
)";
cmd.ExecuteNonQuery();
}
connection.Close();
connection.Open();
using (MySqlCommand insertCommand = connection.CreateCommand())
{
insertCommand.CommandText =
@"INSERT INTO test (test1, test2, test3, test4)
VALUES (@test1, @test2, @test3, @test4)";
insertCommand.Parameters.Add(new MySqlParameter("@test1", DbType.Decimal));
insertCommand.Parameters.Add(new MySqlParameter("@test2", DbType.Decimal));
insertCommand.Parameters.Add(new MySqlParameter("@test3", DbType.Decimal));
insertCommand.Parameters.Add(new MySqlParameter("@test4", DbType.Decimal));
string[] files = Directory.GetFiles("D:/computer/csv files", "*.csv");
foreach (string file in files)
{
string[] lines = System.IO.File.ReadAllLines(file);
bool parse = false;
foreach (string tmpLine in lines)
{
string line = tmpLine.Trim();
if (!parse && line.StartsWith("test1"))
{
parse = true;
continue;
}
if (!parse || string.IsNullOrEmpty(line))
{
continue;
}
foreach (MySqlParameter parameter in insertCommand.Parameters)
{
parameter.Value = null;
}
string[] values = line.Split(new[] { ',' });
for (int i = 0; i < values.Length; i++)
{
MySqlParameter param = insertCommand.Parameters[i];
decimal value;
param.Value = decimal.TryParse(values[i], out value) ? value : 0;
}
insertCommand.ExecuteNonQuery();
}
}
}
connection.Close();
}
}
}
どれガイダンスやヒントを歓迎です!私はあなたの位置にあった場合
フロートに減少させることができますか?リアル??? – Viru