C#で作成
using System;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Windows;
using System.IO;
using Oracle.DataAccess.Client;
namespace Ora2CSV
{
/// <summary>
/// MainWindow.xaml の相互作用ロジック
/// </summary>
public partial class MainWindow : Window
{
public MainWindow() {
InitializeComponent();
}
private void button_Click(object sender, RoutedEventArgs e) {
using(var conn = new OracleConnection(ConfigurationManager.ConnectionStrings["LogisConnStr"].ToString())) {
conn.Open();
//データ取得
var cmd = new OracleCommand();
cmd.Connection = conn;
cmd.CommandText = "SELECT * FROM " + txtTable.Text;
var dr = cmd.ExecuteReader();
var Enc = Encoding.GetEncoding("Shift-JIS");
var wr = new StreamWriter(@"c:\temp\" + txtTable.Text + ".csv", false, Enc);
string strRec = "";
for (int i = 0; i < dr.FieldCount - 1; i++) {
strRec = strRec + @"""" + dr.GetName(i).ToString() + @"""" + ",";
}
//行末の[,]を削除
strRec = strRec.Remove(strRec.Length - 1, 1);
//ヘッダ1行出力
wr.WriteLine(strRec);
//データ行
while(dr.Read()) {
strRec = "";
for (int i = 0; i < dr.FieldCount - 1; i++) {
string vName = dr.GetName(i);
string vType = dr.GetProviderSpecificFieldType(i).ToString();
if (dr.GetProviderSpecificFieldType(i).ToString() == "Oracle.DataAccess.Types.OracleDecimal") {
strRec = strRec + dr[i].ToString() + ",";
}
else {
strRec = strRec + @"""" + dr[i].ToString() + @"""" + ",";
}
}
//行末の[,]を削除
strRec = strRec.Remove(strRec.Length - 1, 1);
//データ1行出力
wr.WriteLine(strRec);
}
wr.Close();
conn.Close();
MessageBox.Show("Complete!");
}
}
}
}
0 件のコメント:
コメントを投稿