我有一个Excel文件WKDEL.xls
。我想读取所有记录并将它们导入到我已经创建的SQL表中。
示例Excel文件:
ID | FNAME | T_W_TON | Y_D_TON | WKEND
--------+--------------+----------+----------+--------------
8118.00 | TOM, COLLIIN | 0.00000 | 0.00000 | 12/8/2018
3.00 | JOHN, DOE | 60.22500 | 60.22500 | 12/8/2018
8.00 | KIM, JUNG | 0.00000 | 0.0000 | 12/8/2018
我需要所有具有T_W_TON
并且基本上大于0的记录并将它们导入到我的SQL表中:
IDCODE: real
FNAME : nvarchar(50)
T_W_TON: real
Y_D_TON: real
WEKEND: date
CROP_SEASON: int(3 as default)
到目前为止,我已经能够加载excel文件,但不确定如何遍历值。 for循环上的代码错误。我也想跳过标题值。
private void GetWeeklyDeliveries()
{
string FilePath = textBox1.Text;
using (var fileStream = new FileStream(FilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
using (var rdr = ExcelReaderFactory.CreateReader(fileStream))
{
using (Stream fos = File.Open(@"C:\Fuel\ZINVOW.dbf", FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
//Create New File or Open New File.
var writer = new DBFWriter();
var bsi_code = new DBFField("HIS_ID", NativeDbType.Char, 25, 0);
var f_name = new DBFField("FNAME", NativeDbType.Char, 50, 0);
var t_w_ton = new DBFField("T_W_TON", NativeDbType.Char, 25, 0);
var y_d_ton = new DBFField("Y_D_TON", NativeDbType.Char, 25, 0);
var w_k_end = new DBFField("WKEND", NativeDbType.Char, 25, 0);
var loc = new DBFField("LOC", NativeDbType.Char, 20, 0);
var b_name = new DBFField("B_NAME", NativeDbType.Char, 50, 0);
var a_name = new DBFField("A_NAME", NativeDbType.Char, 50, 0);
var fuel_rate = new DBFField("FUELRATE", NativeDbType.Numeric, 25, 5);
var payment = new DBFField("PAYMENT", NativeDbType.Numeric, 25, 5);
var hun_fig = new DBFField("HUNFIG", NativeDbType.Char, 12, 0);
var ten_fig = new DBFField("TENFIG", NativeDbType.Char, 12, 0);
var one_fig = new DBFField("ONEFIG", NativeDbType.Char, 12, 0);
var hundred = new DBFField("HUNDRED", NativeDbType.Char, 12, 0);
var teen = new DBFField("TEEN", NativeDbType.Char, 12, 0);
var ten = new DBFField("TEN", NativeDbType.Char, 12, 0);
var one = new DBFField("ONE", NativeDbType.Char, 12, 0);
var words = new DBFField("WORDS", NativeDbType.Char, 50, 0);
var authority = new DBFField("AUTHORITY", NativeDbType.Char, 12, 0);
var thoufig = new DBFField("THOUFIG", NativeDbType.Char, 12, 0);
var thousand = new DBFField("THOUSAND", NativeDbType.Char, 12, 0);
var newrate = new DBFField("NEWRATE", NativeDbType.Numeric, 25, 5);
var sn = new DBFField("SN", NativeDbType.Numeric, 25, 0);
var lic = new DBFField("LIC", NativeDbType.Char, 12, 0);
var dealloc = new DBFField("DELALOC", NativeDbType.Char, 12, 0);
var fee = new DBFField("FEE", NativeDbType.Numeric, 12, 5);
writer.Fields = new[] { bsi_code, f_name, t_w_ton, y_d_ton, w_k_end, loc, b_name, a_name, fuel_rate, payment, hun_fig, ten_fig, one_fig, hundred, teen, ten, one, words, authority, thoufig, thousand, newrate, sn, lic, dealloc, fee };
while (rdr.Read())
{//Begin Reading Delivery File (XLS)
var bsicode = rdr[0];
var fname = rdr[1];
var twton = rdr[2];
var ydton = rdr[3];
var wkend = rdr[4];
if (bsicode.ToString() == "HIS_ID")
{
//Skip First Record
}
else if(Convert.ToDouble(twton.ToString()) > 0)
{
var bsicodex = bsicode.ToString();
var fnamex = fname.ToString();
var t_w_tonx = twton.ToString();
var y_d_tonx = ydton.ToString();
var w_k_endx = wkend.ToString();
var locx = "";
var b_namex = "";
var a_namex = "";
var fuel_ratex = "";
var paymentx = "70.00";//need to Calculate
var hun_figx = "";
var ten_figx = "";
var one_figx = "";
var hundredx = "";
var teenx = "";
var tenx = "";
var onex = "";
var wordsx = "";
var authorithyx = "";
var thoufigx = "";
var thousandx = "";
var newratex = 1.19 * 6; //Need to Calculate
var snx = 1234; //Need to autogenerate can Be Primary Key
var licx = "";
var deallocx = "";
var feex = 1.3*34; //Need to Calculate
writer.AddRecord(bsicodex, fnamex, t_w_tonx, y_d_tonx, w_k_endx, locx, b_namex, a_namex, fuel_ratex.ToString(), paymentx, hun_figx, ten_figx, one_figx, hundredx, teenx, tenx, onex, wordsx, authorithyx, thoufigx, thousandx, newratex.ToString(),snx,licx,deallocx,feex.ToString());
}
}
writer.Write(fos);//CRASHES HERE
}
}
}
答案 0 :(得分:2)
我的操作方式是使用易于使用且功能强大的工具ExcelDataReader
from nuget
using (var fileStream = new FileStream(_fileLocation, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
using (var rdr = ExcelReaderFactory.CreateReader(fileStream))
{
while(rdr.Read())
{
//add it to a data table
}
}
然后使用SqlBulkCopy
将DataTable
写入服务器
答案 1 :(得分:0)
我将跳过数据表并使用DbDataReader。数据表会增加很多开销,在这种情况下,解构它们将像使用数据读取器并逐列处理一样多。如果列对齐,则可以在插入过程中将它们逐列。如果没有,则进行细微调整以使其起作用。
类似这样的事情应该可以解决:
using (SqlConnection icon = new SqlConnection(sqlConnectionString))
{
icon.Open();
using (SqlCommand icmd = new SqlCommand(@"insert into table " +
"values (@ID, @FNAME, @TW, @YD, @WKEND)", icon))
{
icmd.Parameters.Add(new SqlParameter("@ID", SqlDbType.Real));
icmd.Parameters.Add(new SqlParameter("@FNAME", SqlDbType.NVarChar));
icmd.Parameters.Add(new SqlParameter("@TW", SqlDbType.Real));
icmd.Parameters.Add(new SqlParameter("@YD", SqlDbType.Real));
icmd.Parameters.Add(new SqlParameter("@WKEND", SqlDbType.Date));
using (OleDbConnection con = new OleDbConnection(connstring))
{
con.Open();
using (OleDbCommand cmd = new OleDbCommand("select * from [Sheet1$]", con))
{
using (OleDbDataReader reader = cmd.ExecuteReader())
{
reader.Read(); // Skip Header Row
while (reader.Read())
{
for (int i = 0; i < 5; i++)
icmd.Parameters[i].Value = reader.GetValue(i);
icmd.ExecuteNonQuery();
}
reader.Close();
}
}
}
}
}
.GetValue(x)
对象将被读取为字符串,因此可能需要进行一些强制转换/转换才能将它们转换为实数...不确定。我认为这取决于ADO适配器,而且我不确定SQL Server的行为。有些比其他人宽容。