我不是程序员,但尝试从互联网上做一些研究。这里我尝试使用asp.net c#将excel数据导入sql数据库。我从 Internet 获得了一个关于此的概念,但我收到类似这样的错误消息“编译器错误消息:CS1501:方法'有效'没有重载需要 1 个参数”我的代码详细信息如下:
<table>
<tr>
<td>Loan History File Format :</td>
<td><input type="button" value="Download" onclick='location.href = "CardDefaulterTemplete.xlsx#TableName"'/></td>
<td rowspan="2">Before upload data, you must follow this file format. </td>
</tr>
<tr>
<td> <asp:FileUpload ID="fileUpload1" runat="server" Visibl="False"/></td>
<td> <asp:Button ID="btnCardDefaulter" runat="server" Text="Upload"
onclick="btnCardDefaulter_Click"/></td>
</tr>
</table>
<div>
<asp:Button ID="btnExcel" runat="server" Text="Excel"
onclick="btnExcel_Click" />
<asp:Label ID="lblmsg" runat="server" Width="500px"></asp:Label>
</div>
c#代码”
protected string valid(SqlDataReader myreader, int stval)//if any columns are found null then they are replaced by zero
{
object val = myreader[stval];
if (val != DBNull.Value)
return val.ToString();
else
return Convert.ToString(0);
}
public void insertdataintosql(string LHAcNo, string LhAcName, int LHIntRate, string LHDrawingPower, int LHtotalDisbAmt
, int LHEMI, string LHExpiryDate, string LHSanctionDate, string LHDisbDate, int LHOutstandingAmt, int LHPaidAmt
, int LHLastMonthPaid, string LHStatus, string LhCLStatus)
{
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["OptimaWebCustomerQueryCon"].ConnectionString))
{
conn.Open();
string str = @"insert TblLoanHistory (LHAcNo
,LhAcName
,LHIntRate
,LHDrawingPower
,LHtotalDisbAmt
,LHEMI
,LHExpiryDate
,LHSanctionDate
,LHDisbDate
,LHOutstandingAmt
,LHPaidAmt
,LHLastMonthPaid
,LHStatus
,LhCLStatus) values(@LHAcNo
,@LhAcName
,@LHIntRate
,@LHDrawingPower
,@LHtotalDisbAmt
,@LHEMI
,@LHExpiryDate
,@LHSanctionDate
,@LHDisbDate
,@LHOutstandingAmt
,@LHPaidAmt
,@LHLastMonthPaid
,@LHStatus
,@LhCLStatus)";
using (SqlCommand cmd = new SqlCommand(str, conn))
{
cmd.Parameters.Add("@LHAcNo",LHAcNo);
cmd.Parameters.Add("@LhAcName",LhAcName);
cmd.Parameters.Add("@LHIntRate",LHIntRate);
cmd.Parameters.Add("@LHDrawingPower",LHDrawingPower);
cmd.Parameters.Add("@LHtotalDisbAmt",LHtotalDisbAmt);
cmd.Parameters.Add("@LHEMI",LHEMI);
cmd.Parameters.Add("@LHExpiryDate",LHExpiryDate);
cmd.Parameters.Add("@LHSanctionDate",LHSanctionDate);
cmd.Parameters.Add("@LHDisbDate",LHDisbDate);
cmd.Parameters.Add("@LHOutstandingAmt",LHOutstandingAmt);
cmd.Parameters.Add("@LHPaidAmt",LHPaidAmt);
cmd.Parameters.Add("@LHLastMonthPaid",LHLastMonthPaid);
cmd.Parameters.Add("@LHStatus",LHStatus);
cmd.Parameters.Add("@LhCLStatu",LhCLStatus);
cmd.ExecuteNonQuery();
}
conn.Close();
}
}
protected void btnExcel_Click(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["OptimaWebCustomerQueryCon"].ConnectionString))
try
{
string tmpPath = string.Format(@"{0}{1}.xlsx", (@"e:\temp\"), DateTime.Now.ToString("ddMMyyyy"));
if (File.Exists(tmpPath)) File.Delete(tmpPath);
if (fileUpload1.HasFile)
fileUpload1.SaveAs(tmpPath);
FileInfo file = new FileInfo(tmpPath);
ExcelPackage pkg = new ExcelPackage(file);
var excelApp = pkg.Workbook.Worksheets["main_info"];
using (SqlCommand com = new SqlCommand("select LHAcNo, LhAcName, LHIntRate, LHDrawingPower, LHtotalDisbAmt, LHEMI, LHExpiryDate, LHSanctionDate, LHDisbDate, LHOutstandingAmt, LHPaidAmt, LHLastMonthPaid, LHStatus, LhCLStatus from [Template$]", conn))
{
conn.Open();
using (SqlDataReader dr = com.ExecuteReader())
{
string Call_LHAcNo = "";
string Call_LhAcName = "";
int Call_LHIntRate = 0;
string Call_LHDrawingPower = "";
int Call_LHtotalDisbAmt = 0;
int Call_LHEMI = 0;
string Call_LHExpiryDate = "";
string Call_LHSanctionDate = "";
string Call_LHDisbDate = "";
int Call_LHOutstandingAmt =0;
int Call_LHPaidAmt =0;
int Call_LHLastMonthPaid = 0;
string Call_LHStatus = "";
string Call_LhCLStats = "";
while (dr.Read())
{
Call_LHAcNo = valid(dr[1]);
Call_LhAcName = valid(dr[2]);
Call_LHIntRate = valid(dr[3]);
Call_LHDrawingPower = valid(dr[4]);
Call_LHtotalDisbAmt = valid(dr[5]);
Call_LHEMI = valid(dr[6]);
Call_LHExpiryDate = valid(dr[7]);
Call_LHSanctionDate = valid(dr[8]);
Call_LHDisbDate = valid(dr[9]);
Call_LHOutstandingAmt = valid(dr[10]);
Call_LHPaidAmt = valid(dr[11]);
Call_LHLastMonthPaid = valid(dr[12]);
Call_LHStatus = valid(dr[13]);
Call_LhCLStats = valid(dr[14]);
insertdataintosql(Call_LHAcNo,
Call_LhAcName,
Call_LHIntRate,
Call_LHDrawingPower,
Call_LHtotalDisbAmt,
Call_LHEMI,
Call_LHExpiryDate,
Call_LHSanctionDate,
Call_LHDisbDate,
Call_LHOutstandingAmt,
Call_LHPaidAmt,
Call_LHLastMonthPaid,
Call_LHStatus,
Call_LhCLStats);
}
conn.Close();
}
}
}
catch (DataException ee)
{
lblmsg.Text = ee.Message;
lblmsg.ForeColor = System.Drawing.Color.Red;
}
finally
{
lblmsg.Text = "Data Inserted Sucessfully";
lblmsg.ForeColor = System.Drawing.Color.Green;
}
}
我对“ protected string valid(SqlDataReader
myreader, int stval)
”和“下”protected void btnExcel_Click(object
sender, EventArgs e)
从 excel 导入日期和处理 while 循环感到困惑。