c# - 通过 Oledb c# 防止重复数据从 Excel 到 db
问题描述
我有一个 excel,我想用一个按钮将其中的四列上传到 SQL 表。问题是当我重复单击按钮时,所有数据都将被复制,但我不希望这样。我只想更新新数据。我的查询:
protected void Button1_Click(object sender, EventArgs e)
{
int UserID;
int InsuID;
string Result;
int Year;
//** مسیر فایل اکسل**
String ExcelPath = @"D:\Insu_lab.xlsx";
//** کانکشن به آفیس**
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 cmd = new OleDbCommand("select * from [Sheet1$]", mycon);
OleDbDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
UserID = Convert.ToInt32(dr[0].ToString());
InsuID = Convert.ToInt32(dr[1].ToString());
Result = dr[2].ToString();
Year = Convert.ToInt32(dr[3].ToString());
savedata(UserID, InsuID, Result, Year);
Label1.Text = "اطلاعات با موفقیت در دیتابیس ذخیره شد";
}
}
private void savedata(int UserID, int InsuID, string Result, int Year)
{
String query = "insert into tbl_Result(UserID,InsuID,Result,Year) values(" + UserID + ",'" + InsuID + "','" + Result + "','" + Year + "') ";
String mycon = "Data Source=MC6082; Initial Catalog=Insurance; Integrated Security=true";
SqlConnection con = new SqlConnection(mycon);
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = query;
cmd.Connection = con;
cmd.ExecuteNonQuery();
}
解决方案
解决方案 1:当您clicked
打开时button
,that( button
) 禁用该按钮。
Button1.disable = true;
导出结束时:
Button1.disable = false;
解决方案2:您可以在这部分使用from jquery ajax。
推荐阅读
- xml - 使用 xslt 2.0 合并具有相同父属性值的元素的 xml 内容
- python - 为什么查看 VSS 会导致 OLE 错误 0x80041014?
- python - 在单词的句子中找到句子
- javascript - 尝试在 React 中的组件之间切换/渲染
- python - 如何散列段落
- python - 如何在具有多个目标变量(多输出)的样本数据上过度/不足?
- javascript - 如何在 axios.all() 中附加标题
- javascript - Flask:加载模块脚本失败:服务器以“text/html”的非 JavaScript MIME 类型响应
- python - Pandas read_html 错误 - NoneType 对象没有属性“项目”
- python - Pandas 中的 ALTER COLUMN 等效项