|
|
|
ช่วยด้วยครับ insert excel เข้า sql server ทำไมถึงขึ้น column out of rang ครับ |
|
|
|
|
|
|
|
ตามหัวข้อครับ ดัก try cath ขึ้น column out of rang
ข้อมูลใน excel
table ที่ insert
โค้ด
Code (C#)
FileInfo excel = new FileInfo((@"D:\Application\Billpayment\FileTest\testbill.xlsx"));
using (var package = new ExcelPackage(excel))
{
var workbook = package.Workbook;
//*** Sheet 1
var worksheet = workbook.Worksheets.First();
string strConnString = "Server=xxxxxxxx;UID=sa;PASSWORD=xxxxx;database=base";
var objConn = new SqlConnection(strConnString);
objConn.Open();
//*** Loop to Insert
int totalRows = worksheet.Dimension.End.Row;
for (int i = 1; i <= totalRows; i++)
{
string strSQL = "INSERT INTO tbl_billpayment_confirm (ref_no_1,ref_no_2,billpayment_name,amount) "
+ " VALUES ("
+ " '" + worksheet.Cells[i, 0].Text.ToString() + "', "
+ " '" + worksheet.Cells[i, 1].Text.ToString() + "', "
+ " '" + worksheet.Cells[i, 2].Text.ToString() + "', "
+ " '" + worksheet.Cells[i, 3].Text.ToString() + "' "
+ ")";
//MessageBox.Show(i.ToString());
var objCmd = new SqlCommand(strSQL, objConn);
objCmd.ExecuteNonQuery();
}
objConn.Close();
MessageBox.Show("เสร็จ");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
Tag : .NET, Win (Windows App), C#
|
|
|
|
|
|
Date :
2018-06-11 14:23:23 |
By :
darkgolfman0 |
View :
627 |
Reply :
2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
แก้ได้แล้วครับปรับจาก
worksheet.Cells[i, 0].Text.ToString()
เป็น
worksheet.Cells[i, 1].Text.ToString()
|
|
|
|
|
Date :
2018-06-11 14:33:38 |
By :
darkgolfman0 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Date :
2018-06-11 15:00:49 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 00
|