我有一个上传按钮,可以上传excel文件并将其保存到我的数据库中。我想发生的是,如果该excel文件中已经存在一个或多个数据,则其他数据也将不被上传,尽管它还不存在。我将其添加到数据库和上传按钮的代码如下。
添加到数据库
private void AddNewTrainee(string strdelname, string strrank, string strcomp, string strcourse, string strcenter, string strinst,
string strsdate, string stredate, string strcissued, string strcnumber, string strremark, int recdeleted, string credate, string update, int fromupload)
{
connection.Open();
String checkDateAndName = "Select count(*) from Trainees where StartDate= '" + strsdate + "' and Delegate='" + strdelname + "' and REC_DELETED = 0 ";
SqlCommand cmd = new SqlCommand(checkDateAndName, connection);
int dataRepeated = Convert.ToInt32(cmd.ExecuteScalar().ToString());
bool boolDataRepated;
connection.Close();
if (!(dataRepeated >= 1))
{
boolDataRepated = false;
}
else
boolDataRepated = true;
connection.Open();
string certNumber = "Select * from CertID_Table update CertID_Table set CertificateID = CertificateID + 1 from CertID_Table ";
SqlCommand cmdCert = new SqlCommand(certNumber, connection);
using (SqlDataReader oReader = cmdCert.ExecuteReader())
{
while (oReader.Read())
{
string test1 = oReader["CertificateID"].ToString();
ViewState["certnumber"] = test1;
}
}
connection.Close();
strcnumber = (string)ViewState["certnumber"];
if (boolDataRepated == false)
{
string path = "D:\\Intern\\BASSWeb\\SQLCommands\\AddSQL.txt";
StringBuilder sb = new StringBuilder();
using (StreamReader sr = new StreamReader(path))
{
while (sr.Peek() >= 0)
{
sb.Append(sr.ReadLine());
}
string sql = sb.ToString();
try
{
connection.Open();
SqlCommand cmd1 = new SqlCommand(sql, connection);
cmd1.Parameters.AddWithValue("@delName", strdelname);
cmd1.Parameters.AddWithValue("@rank", strrank);
cmd1.Parameters.AddWithValue("@comp", strcomp);
cmd1.Parameters.AddWithValue("@course", strcourse);
cmd1.Parameters.AddWithValue("@center", strcenter);
cmd1.Parameters.AddWithValue("@instructor", strinst);
cmd1.Parameters.AddWithValue("@sdate", strsdate);
cmd1.Parameters.AddWithValue("@edate", stredate);
cmd1.Parameters.AddWithValue("@cissued", strcissued);
cmd1.Parameters.AddWithValue("@cnumber", strcnumber);
cmd1.Parameters.AddWithValue("@remark", strremark);
cmd1.Parameters.AddWithValue("@rdeleted", recdeleted);
cmd1.Parameters.AddWithValue("@cdate", credate);
cmd1.Parameters.AddWithValue("@udate", update);
cmd1.Parameters.AddWithValue("@fupload", fromupload);
cmd1.CommandType = CommandType.Text;
cmd1.ExecuteNonQuery();
}
catch (System.Data.SqlClient.SqlException ex)
{
string msg = "Insert/Update Error:";
msg += ex.Message;
throw new Exception(msg);
}
finally
{
connection.Close();
}
}
}
else
{
string script = "alert(\"The data already exists\");";
ScriptManager.RegisterStartupScript(this, GetType(), "ServerControlScript", script, true);
}
}
上传按钮
protected void btnUpload_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
try
{
string path = Path.GetFileName(FileUpload1.FileName);
path = path.Replace(" ", "");
FileUpload1.SaveAs(Server.MapPath("~/Datas/") + path);
String ExcelPath = Server.MapPath("~/Datas/") + path;
OleDbConnection mycon = new OleDbConnection("Provider = Microsoft.ACE.OLEDB.12.0; Data Source = " + ExcelPath + "; Extended Properties=Excel 8.0; Persist Security Info = False");
mycon.Open();
OleDbCommand cmdX = new OleDbCommand("select * from [Sheet1$]", mycon);
OleDbDataReader dr = cmdX.ExecuteReader();
while (dr.Read())
{
delegateName = dr[0].ToString();
rankPos = dr[1].ToString();
company = dr[2].ToString();
courseTitle = dr[3].ToString();
trainingCenter = dr[4].ToString();
instructor = dr[5].ToString();
staDa = DateTime.Parse(dr[6].ToString());
string startDate = staDa.ToString("MM/dd/yyyy");
endDa = DateTime.Parse(dr[7].ToString());
string endDate = endDa.ToString("MM/dd/yyyy");
certIssued = dr[8].ToString();
certNum = dr[9].ToString();
remarks = dr[10].ToString();
recDeleted = 0;
dateCreated = DateTime.Now.ToString("MM/dd/yyyy HH:mm");
dateUpdated = string.Empty;
fromUpload = 1;
AddNewTrainee(delegateName, rankPos, company, courseTitle, trainingCenter, instructor,
startDate, endDate, certIssued, certNum, remarks, recDeleted, dateCreated, dateUpdated, fromUpload);
}
}
catch (Exception ex)
{
string errorMessage = "alert(\"ERROR: " + ex.Message.ToString() + " \");";
ScriptManager.RegisterStartupScript(this, GetType(), "ServerControlScript", errorMessage, true);
}
}
else
{
string errorMessage = "alert(\"ERROR: You have not specified a file \");";
ScriptManager.RegisterStartupScript(this, GetType(), "ServerControlScript", errorMessage, true);
}
PopulateData();
}
答案 0 :(得分:0)
您必须将transferMode设置为“ Streamed”,否则将始终获得一个文件。
看看这篇文章:https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/how-to-enable-streaming
答案 1 :(得分:0)
我认为您需要解决一些事情才能达到最终目标。
研究一下我发布的链接。乍一看,交易似乎有些不知所措,但实际上却非常简单。也许我可以帮助您开始了解。真的只有三个额外的步骤;
1。 创建命令后,初始化事务对象。
SqlTransaction transaction = connection.BeginTransaction();
2。 在您的所有Sql命令(插入,更新,删除等)上附加事务。
cmd.Transaction = transaction;
这将允许您执行而不实际将SqlCommands放入数据库中。最后,在处理完所有插入和更新后,您可以执行最后一步。 using语句不是必需的,只是很好的做法。这可能是您接下来想了解的非常有用的东西。
3。 将所有SqlCommand提交到数据库。
transaction.Commit();
如果在数据处理过程中的任何时候出了点问题,您都无法回滚这样的每个事务。
transaction.Rollback();