【发布时间】:2015-06-03 15:18:31
【问题描述】:
我正在编写一个 C# windows 应用程序,但我似乎不知道如何将数据从 CSV 文件插入到数据库中。
我有一个名为“data.csv”的 csv 文件,其记录如下
Header 201501
id code amount
1 ab11 5000
2 ab11 6000
3 ab11 8000
3 wx34 2500
3 df21 1000
4 ab11 7000
4 zx54 3500
我必须将这些数据放到 sql 表中,如下所示
ab11 表示要支付的费用 任何其他代码代表学生已支付的金额
第一个表 tblStudents
id code amount
1 ab11 5000
2 ab11 6000
3 ab11 8000
4 ab11 7000
5 ab11 9000
第二张表 tblPaid
id code Paid Month
3 wx34 2500 201401
3 df21 1000 201401
4 zx54 3500 201401
基本上我要做的是从 data.csv 文本文件中捕获这些数据并将其排序到一个表格中,使其看起来像 tblStudents 和 tblPaid。
对于 tblStudents,我必须将代码“ab11”的所有内容写入表格。
对于 tblPaid,我只需要只写没有“ab11”代码的数据,并放入一个读取日期旁边的字段data.csv 文本文件的标题。
我正在考虑使用 BULK INSERT,但标题不应该在表格中。
所以我只能创建表格并读取 data.csv 文本文件,如下所示
//Open File Dialog to open csv file
//Only get csv files
openFileDialog1.Filter = ".csv file|*.csv*";
openFileDialog1.FilterIndex = 1;
DialogResult result = openFileDialog1.ShowDialog();
if (result == DialogResult.Yes)
{
String file = openFileDialog1.FileName;
//Read csv text file to get the month in the first line
using (var reader = new StreamReader(file))
{
string firstLine = reader.ReadLine();
string[] splitedLine = firstLine.Split(';');
string month = splitedLine[1];
}
try
{
//String with connection information of the database PSAHoldings
string connect = "Data Source=BRIAN-PC\\SQLEXPRESS; Initial Catalog=PSAHoldings; user id =sa; Password=kagiso";
//String Query to create t_original table if it does not exist already
string table = "IF NOT EXISTS (SELECT * FROM sysobjects WHERE name='t_original' AND xtype='U')" +
"CREATE TABLE t_original (" +
"empId varChar(10) NULL," +
"paycode varChar(10) NULL," +
"amount int NULL," +
")";
//String Query to create tblStudents table if it does not exist already
string tblStudents = "IF NOT EXISTS (SELECT * FROM sysobjects WHERE name='t_employee' AND xtype='U')" +
"CREATE TABLE t_employee (" +
"empId varChar(10) NOT NULL PRIMARY KEY," +
"paycode varChar(10) NULL," +
"amount int NULL," +
")";
//String Query to create tblPaid table if it does not exist already
string Paid = "IF NOT EXISTS (SELECT * FROM sysobjects WHERE name='t_deduction' AND xtype='U')" +
"CREATE TABLE t_deduction (" +
"empId varChar(10) NOT NULL PRIMARY KEY," +
"amount int NULL," +
"balance int NULL," +
")";
//String Query to create t_institutions table if it does not exist already
string t_institutions = "IF NOT EXISTS (SELECT * FROM sysobjects WHERE name='t_institutions' AND xtype='U')" +
"CREATE TABLE t_institutions (" +
"empId varChar(10) NOT NULL PRIMARY KEY," +
"paycode varChar(10) NOT NULL," +
"amount int NULL," +
")";
//Connecting to the server/database
SqlConnection con = new SqlConnection(connect);
con.Open();
//Create the tables
SqlCommand createTable = new SqlCommand(table, con);
SqlCommand createEmployee = new SqlCommand(t_employee, con);
SqlCommand createDeductions = new SqlCommand(t_deduction, con);
SqlCommand createInstitution = new SqlCommand(t_institutions, con);
createTable.ExecuteNonQuery();
createEmployee.ExecuteNonQuery();
createDeductions.ExecuteNonQuery();
createInstitution.ExecuteNonQuery();
//String Query to insert the text file into the t_original table
String BulkInsert = "BULK INSERT t_original FROM_" +
file + "_WITH (--FIRSTROW = 3," +
"FIELDTERMINATOR = ''," +
"MAXERRORS = 0," +
"ROWTERMINATOR = '\\n')";
//"UPDATE t_original"+
//"SET month =" + month +
//"WHERE month is null";
//insert the text file into the t_original table
SqlCommand bulkCmd = new SqlCommand(BulkInsert, con);
bulkCmd.ExecuteNonQuery();
}
//Catch exeption
catch (SqlException ex)
{
MessageBox.Show(ex.ToString(), "Exception Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
我已经写了一个BULK_INSERT,但我的表中没有任何东西
任何帮助都将不胜感激,因为我是开发初学者
【问题讨论】:
-
检查 SqlBulkCopy msdn.microsoft.com/en-us/library/…
-
另外,BULK INSERT 有
FIRST_ROW=子句允许它跳过文件开头的一些行。 -
为什么第二个表中有'ab11'的记录?
-
@Steve 抱歉,我已经纠正了我的打字错误。第二个表不应该有“ab11”
-
@RBarryYoung 谢谢你,我不知道如何跳过第一行。因此,既然我假设 FIRST_ROW 子句仅适用于第一行而不适用于第二行,那么是否可以跳到行?
标签: c# sql sql-server sorting