概要
cscの作法、調べてみた。
練習問題、やってみた。
練習問題
csvを読んでsqliteにinsertせよ。
サンプルコード
using System;
using System.Text;
using System.IO;
using System.Data.SQLite;
namespace App
{
public class Program {
public static void Main(string[] args) {
SQLiteConnectionStringBuilder ConnectionStr = new SQLiteConnectionStringBuilder();
ConnectionStr.DataSource = "db0.sqlite";
using (SQLiteConnection Connection = new SQLiteConnection(ConnectionStr.ToString()))
{
Connection.Open();
using(SQLiteCommand Command = new SQLiteCommand(Connection))
{
Encoding enc = Encoding.GetEncoding("Shift_JIS");
string file = "out.csv";
StreamReader reader = new StreamReader(file, enc);
string temp = reader.ReadToEnd();
string[] lines = temp.Split(new string[] {
"\r\n"
}, StringSplitOptions.None);
string[] spLine;
for (int i = 1; i < lines.Length; i++)
{
spLine = lines[i].Split(new char[] {
',' ,
'\t'
}, StringSplitOptions.None);
for (int j = 0; j < spLine.Length; j++)
{
spLine[j] = spLine[j].Trim('"');
}
//Console.WriteLine(spLine[2]);
Command.CommandText = "INSERT INTO adata(stime, title, price, biko) VALUES (:stime, :title, :price, :biko)";
Command.Parameters.Add(new SQLiteParameter("stime", spLine[1]));
Command.Parameters.Add(new SQLiteParameter("title", spLine[2]));
Command.Parameters.Add(new SQLiteParameter("price", spLine[3]));
Command.Parameters.Add(new SQLiteParameter("biko", spLine[4]));
Command.ExecuteNonQuery();
}
}
}
Console.WriteLine("ok0");
}
}
}
以上。