作者:快乐伴随su1 | 来源:互联网 | 2023-07-31 17:04
我想从文本文件中读取并将其第一列作为行插入数据库中.这是文本文件的样子:1264311|SMS_DR|DELIVERED373414|SMS_DR|EXPIRED418|SMS_D
我想从文本文件中读取并将其第一列作为行插入数据库中.
这是文本文件的样子:
1264311 | SMS_DR | DELIVERED
373414 | SMS_DR | EXPIRED
418 | SMS_DR | UNDELIVERABLE
1628984 | SMS_MT |
我想只获取带有整数值的第一列,并将它们作为一行插入我的数据库中以获得:
DELIVERED EXPIRED UNDELIVERABLE BLABLA
1264311 373414 418 1628984
当我运行我的代码时,它会插入所有4行,这是我的代码:
sr1 = new StreamReader("C:\\Users\\adamoui\\Desktop\\Statjbm_20161009.txt");
string[] allLines1 = File.ReadAllLines("C:\\Users\\adamoui\\Desktop\\Statjbm_20161009.txt");
for (int i = 0; i {
string[] column = allLines1[i].Split(new char[] { '|' });
SqlCommand cmdJBM = new SqlCommand("INSERT INTO dbo.StatJBM_NEW (Noeud, Total_MT, Date, DELIVERED, EXPIRED, UNDELIVERABLE) VALUES (@Noeud, @Total_MT, @Date, @DELIVERED, @EXPIRED, @UNDELIVERABLE)", con);
cmdJBM.Parameters.AddWithValue("@Noeud", "JBM");
cmdJBM.Parameters.AddWithValue("@DELIVERED", column[0]);
cmdJBM.Parameters.AddWithValue("@EXPIRED", column[0]);
cmdJBM.Parameters.AddWithValue("@UNDELIVERABLE", column[0]);
cmdJBM.Parameters.AddWithValue("@Total_MT", column[0]);
cmdJBM.Parameters.AddWithValue("@Date", DateTime.Now.AddDays(-1));
cmdJBM.ExecuteNonQuery();
}
con.Close();
结果:
解决方法:
你需要这样的东西:
var delivered = allLines[0].Split(new char[] { '|' })[0];
var expired = allLines[1].Split(new char[] { '|' })[0];
var undeliverable = allLines[2].Split(new char[] { '|' })[0];
....
cmdJBM.Parameters.AddWithValue("@DELIVERED", delivered);
cmdJBM.Parameters.AddWithValue("@EXPIRED", expired);
cmdJBM.Parameters.AddWithValue("@UNDELIVERABLE", undeliverable);