OleDbConnectionも同じかもしれませんが、1行目のデータにコーテーションがあるか無いかで型を決めている節があります。GDD Blog: [.NET]CSVデータを操作する(OleDb)のときとは微妙にデータの出具合が違う様です。
Select時に型の情報を見てみると、int32,String,DateTimeという順で並んでいました。
細かな型指定をしたい場合は、同フォルダにSchema.iniを置くとそれを参考にするらしい。こんど少し調べてみようと思います。
private void button1_Click(object sender, EventArgs e)
{
//using System.Data.Odbc;
string csvDir = @"d:\temp";
string csvFileName = "test.csv";
//ODBC接続文字列を組み立てる
OdbcConnectionStringBuilder builder = new OdbcConnectionStringBuilder();
builder["Driver"] = "Microsoft Text Driver (*.txt; *.csv)";
builder["dbq"] = csvDir;
builder["Extension"] = "asc,csv,tab,txt";
string strSQL = "";
int count = 0;
using (OdbcConnection con = new OdbcConnection(builder.ConnectionString))
{
con.Open();
strSQL = "SELECT count(*) FROM " + csvFileName;
using (OdbcCommand cmd = new OdbcCommand(strSQL, con))
{
//スカラー値を取得します
object obj = cmd.ExecuteScalar();
count = (int)obj;
StringBuilder sb = new StringBuilder();
sb.AppendLine("--------------------------------------");
sb.Append("実行結果 [").Append(obj).Append("]\r\n");
System.Diagnostics.Debug.WriteLine(sb.ToString());
}
strSQL = "SELECT MAX(idx) FROM " + csvFileName;
using (OdbcCommand cmd = new OdbcCommand(strSQL, con))
{
//スカラー値を取得します
object obj = cmd.ExecuteScalar();
StringBuilder sb = new StringBuilder();
sb.AppendLine("--------------------------------------");
sb.Append("実行結果 [").Append(obj).Append("]\r\n");
System.Diagnostics.Debug.WriteLine(sb.ToString());
}
strSQL = "INSERT INTO " + csvFileName + " values(" + count++ + ", 'abc', '" + DateTime.Now.ToString() + "')";
using (OdbcCommand cmd = new OdbcCommand(strSQL, con))
{
//更新処理を実行します
int result = cmd.ExecuteNonQuery();
StringBuilder sb = new StringBuilder();
sb.AppendLine("--------------------------------------");
sb.Append("登録件数 [").Append(result).Append("]\r\n");
System.Diagnostics.Debug.WriteLine(sb.ToString());
}
//if (count > 0)
//{
// strSQL = "DELETE " + csvFileName + " WHERE idx='" + count + "'";
// using (OdbcCommand cmd = new OdbcCommand(strSQL, con))
// {
// //更新処理を実行します
// int result = cmd.ExecuteNonQuery();
// StringBuilder sb = new StringBuilder();
// sb.AppendLine("--------------------------------------");
// sb.Append("更新件数 [").Append(result).Append("]\r\n");
// System.Diagnostics.Debug.WriteLine(sb.ToString());
// }
// //strSQL = "UPDATE " + csvFileName + " SET text1 = 'def' WHERE idx='" + count + "'";
// strSQL = "UPDATE " + csvFileName + " SET text1 = 'def'";
// using (OdbcCommand cmd = new OdbcCommand(strSQL, con))
// {
// //更新処理を実行します
// int result = cmd.ExecuteNonQuery();
// StringBuilder sb = new StringBuilder();
// sb.AppendLine("--------------------------------------");
// sb.Append("更新件数 [").Append(result).Append("]\r\n");
// System.Diagnostics.Debug.WriteLine(sb.ToString());
// }
//}
strSQL = "SELECT * FROM " + csvFileName; //+ " WHERE idx<3";
using (OdbcCommand cmd = new OdbcCommand(strSQL, con))
using (OdbcDataReader rd = cmd.ExecuteReader())
{
PrintData(rd);
}
}
}
private void PrintData(OdbcDataReader rd)
{
StringBuilder sb = new StringBuilder();
//カラム名を取得し、カンマ区切りで編集します
sb.AppendLine("--------------------------------------");
for (int cnt = 0; cnt < rd.FieldCount; cnt++)
{
sb.Append(rd.GetName(cnt)).Append(",");
}
sb.Remove(sb.Length - 1, 1);
sb.AppendLine("");
sb.AppendLine("--------------------------------------");
//データを取得しカンマ区切りで編集します
for (int row = 0; rd.Read(); row++)
{
for (int col = 0; col < rd.FieldCount; col++)
{
sb.Append(rd.GetValue(col)).Append(",");
}
sb.Remove(sb.Length - 1, 1);
sb.AppendLine("");
}
sb.AppendLine("--------------------------------------");
System.Diagnostics.Debug.WriteLine(sb.ToString());
}
{
//using System.Data.Odbc;
string csvDir = @"d:\temp";
string csvFileName = "test.csv";
//ODBC接続文字列を組み立てる
OdbcConnectionStringBuilder builder = new OdbcConnectionStringBuilder();
builder["Driver"] = "Microsoft Text Driver (*.txt; *.csv)";
builder["dbq"] = csvDir;
builder["Extension"] = "asc,csv,tab,txt";
string strSQL = "";
int count = 0;
using (OdbcConnection con = new OdbcConnection(builder.ConnectionString))
{
con.Open();
strSQL = "SELECT count(*) FROM " + csvFileName;
using (OdbcCommand cmd = new OdbcCommand(strSQL, con))
{
//スカラー値を取得します
object obj = cmd.ExecuteScalar();
count = (int)obj;
StringBuilder sb = new StringBuilder();
sb.AppendLine("--------------------------------------");
sb.Append("実行結果 [").Append(obj).Append("]\r\n");
System.Diagnostics.Debug.WriteLine(sb.ToString());
}
strSQL = "SELECT MAX(idx) FROM " + csvFileName;
using (OdbcCommand cmd = new OdbcCommand(strSQL, con))
{
//スカラー値を取得します
object obj = cmd.ExecuteScalar();
StringBuilder sb = new StringBuilder();
sb.AppendLine("--------------------------------------");
sb.Append("実行結果 [").Append(obj).Append("]\r\n");
System.Diagnostics.Debug.WriteLine(sb.ToString());
}
strSQL = "INSERT INTO " + csvFileName + " values(" + count++ + ", 'abc', '" + DateTime.Now.ToString() + "')";
using (OdbcCommand cmd = new OdbcCommand(strSQL, con))
{
//更新処理を実行します
int result = cmd.ExecuteNonQuery();
StringBuilder sb = new StringBuilder();
sb.AppendLine("--------------------------------------");
sb.Append("登録件数 [").Append(result).Append("]\r\n");
System.Diagnostics.Debug.WriteLine(sb.ToString());
}
//if (count > 0)
//{
// strSQL = "DELETE " + csvFileName + " WHERE idx='" + count + "'";
// using (OdbcCommand cmd = new OdbcCommand(strSQL, con))
// {
// //更新処理を実行します
// int result = cmd.ExecuteNonQuery();
// StringBuilder sb = new StringBuilder();
// sb.AppendLine("--------------------------------------");
// sb.Append("更新件数 [").Append(result).Append("]\r\n");
// System.Diagnostics.Debug.WriteLine(sb.ToString());
// }
// //strSQL = "UPDATE " + csvFileName + " SET text1 = 'def' WHERE idx='" + count + "'";
// strSQL = "UPDATE " + csvFileName + " SET text1 = 'def'";
// using (OdbcCommand cmd = new OdbcCommand(strSQL, con))
// {
// //更新処理を実行します
// int result = cmd.ExecuteNonQuery();
// StringBuilder sb = new StringBuilder();
// sb.AppendLine("--------------------------------------");
// sb.Append("更新件数 [").Append(result).Append("]\r\n");
// System.Diagnostics.Debug.WriteLine(sb.ToString());
// }
//}
strSQL = "SELECT * FROM " + csvFileName; //+ " WHERE idx<3";
using (OdbcCommand cmd = new OdbcCommand(strSQL, con))
using (OdbcDataReader rd = cmd.ExecuteReader())
{
PrintData(rd);
}
}
}
private void PrintData(OdbcDataReader rd)
{
StringBuilder sb = new StringBuilder();
//カラム名を取得し、カンマ区切りで編集します
sb.AppendLine("--------------------------------------");
for (int cnt = 0; cnt < rd.FieldCount; cnt++)
{
sb.Append(rd.GetName(cnt)).Append(",");
}
sb.Remove(sb.Length - 1, 1);
sb.AppendLine("");
sb.AppendLine("--------------------------------------");
//データを取得しカンマ区切りで編集します
for (int row = 0; rd.Read(); row++)
{
for (int col = 0; col < rd.FieldCount; col++)
{
sb.Append(rd.GetValue(col)).Append(",");
}
sb.Remove(sb.Length - 1, 1);
sb.AppendLine("");
}
sb.AppendLine("--------------------------------------");
System.Diagnostics.Debug.WriteLine(sb.ToString());
}
■実行結果
--------------------------------------
実行結果 [6]
--------------------------------------
実行結果 [5]
--------------------------------------
登録件数 [1]
--------------------------------------
idx,text1,text2
--------------------------------------
0,abc,2009/09/06 22:44:44
1,abc,2009/09/06 22:49:58
2,abc,2009/09/06 22:49:58
3,abc,2009/09/06 22:50:00
4,abc,2009/09/06 22:50:00
5,abc,2009/09/06 22:50:01
6,abc,2009/09/06 22:50:06
--------------------------------------
■データファイル
idx,text1,text2
0,"abc",2009/09/06 22:44:44
1,"abc",2009/09/06 22:49:58
2,"abc",2009/09/06 22:49:58
3,"abc",2009/09/06 22:50:00
4,"abc",2009/09/06 22:50:00
5,"abc",2009/09/06 22:50:01
6,"abc",2009/09/06 22:50:06
0 件のコメント:
コメントを投稿