しかし、以下のような問題が発生しています。
- 空の状態(ヘッタのみ)からスタートするとidxが文字列型になってしまう(型の指定方法がわからない)
- DELETEとUPDATEができない(CSVなので当然といえば。。。)
まあ、とりえあえずコードはこんな感じ。
private void button1_Click(object sender, EventArgs e)
{
//using System.Data.OleDb;
string csvDir = @"c:\temp";
string csvFileName = "test.csv";
//接続文字列
string conString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + csvDir +
";Extended Properties=\"text;HDR=Yes;FMT=Delimited\"";
string strSQL = "";
int count = 0;
using (OleDbConnection con = new OleDbConnection(conString))
{
con.Open();
strSQL = "SELECT count(*) FROM " + csvFileName;
using (OleDbCommand cmd = new OleDbCommand(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 (OleDbCommand cmd = new OleDbCommand(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 (OleDbCommand cmd = new OleDbCommand(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 (OleDbCommand cmd = new OleDbCommand(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 (OleDbCommand cmd = new OleDbCommand(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<" + count;
using (OleDbCommand cmd = new OleDbCommand(strSQL, con))
using (OleDbDataReader rd = cmd.ExecuteReader())
{
PrintData(rd);
}
}
}
private void PrintData(OleDbDataReader 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.OleDb;
string csvDir = @"c:\temp";
string csvFileName = "test.csv";
//接続文字列
string conString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + csvDir +
";Extended Properties=\"text;HDR=Yes;FMT=Delimited\"";
string strSQL = "";
int count = 0;
using (OleDbConnection con = new OleDbConnection(conString))
{
con.Open();
strSQL = "SELECT count(*) FROM " + csvFileName;
using (OleDbCommand cmd = new OleDbCommand(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 (OleDbCommand cmd = new OleDbCommand(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 (OleDbCommand cmd = new OleDbCommand(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 (OleDbCommand cmd = new OleDbCommand(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 (OleDbCommand cmd = new OleDbCommand(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<" + count;
using (OleDbCommand cmd = new OleDbCommand(strSQL, con))
using (OleDbDataReader rd = cmd.ExecuteReader())
{
PrintData(rd);
}
}
}
private void PrintData(OleDbDataReader 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());
}
}
■実行結果
--------------------------------------
実行結果 [16]
--------------------------------------
実行結果 [15]
--------------------------------------
登録件数 [1]
--------------------------------------
idx,text1,text2
--------------------------------------
0,abc,2009/02/22 5:53:23
1,abc,2009/02/22 5:56:02
2,abc,2009/02/22 6:00:43
3,abc,2009/02/22 6:06:38
4,abc,2009/02/22 6:07:23
5,abc,2009/02/22 6:13:57
6,abc,2009/02/22 6:14:46
7,abc,2009/02/22 6:19:28
8,abc,2009/02/22 6:20:10
9,abc,2009/02/22 6:20:17
10,abc,2009/02/22 6:21:00
11,abc,2009/02/22 6:21:45
12,abc,2009/02/22 6:22:13
13,abc,2009/02/22 6:23:19
14,abc,2009/02/22 6:25:43
15,abc,2009/02/22 6:26:08
--------------------------------------
■データファイル
idx,text1,text2
0,"abc","2009/02/22 5:53:23"
1,"abc","2009/02/22 5:56:02"
2,"abc","2009/02/22 6:00:43"
3,"abc","2009/02/22 6:06:38"
4,"abc","2009/02/22 6:07:23"
5,"abc","2009/02/22 6:13:57"
6,"abc","2009/02/22 6:14:46"
7,"abc","2009/02/22 6:19:28"
8,"abc","2009/02/22 6:20:10"
9,"abc","2009/02/22 6:20:17"
10,"abc","2009/02/22 6:21:00"
11,"abc","2009/02/22 6:21:45"
12,"abc","2009/02/22 6:22:13"
13,"abc","2009/02/22 6:23:19"
14,"abc","2009/02/22 6:25:43"
15,"abc","2009/02/22 6:26:08"
16,"abc","2009/02/22 6:26:32"
0 件のコメント:
コメントを投稿