-
C#教程之C#教程之c# excel如何导入到sqlserver数据库
本站最新发布 C#从入门到精通
试听地址 https://www.xin3721.com/eschool/CSharpxin3721/
试听地址 https://www.xin3721.com/eschool/CSharpxin3721/
最近在做这个如何把excel导入到数据库中,经过多方查找,终于找到一个适合的,并且经过自己的完善可以正常使用(忘记原作者博客的链接地址了,敬请见谅)
- 首先是窗体的创建,文本框显示文件的路径,按钮执行操作,DataGridView显示导入的信息
-
代码如下:可根据自己的需求进行修改,我是要导入之后就对我的另一窗体进行刷新,定义了委托,你们可以忽略。
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122//定义委托
public
delegate
void
Refresh();
//定义事件
public
event
Refresh myRefresh;
public
ExcelRoprtForm()
{
InitializeComponent();
}
private
void
button1_Click(
object
sender, EventArgs e)
{
//选中导入的文件
try
{
//openFileDialog1.Filter = "Excel 文件|*.xls";//指定存放文件格式类型
OpenFileDialog fd =
new
OpenFileDialog();
fd.Filter =
"Excel文件(*.xls,xlsx)|*.xls;*.xlsx"
;
if
(fd.ShowDialog() == DialogResult.OK)
{
string
fileName = fd.FileName.ToString();
this
.textBox1.Text = fileName;
}
}
catch
(Exception ee)
{
MessageBox.Show(
"打开文件出错!"
+ ee.Message.ToString());
}
}
private
DataSet xsldata(
string
filepath)
{
string
strCon =
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
+ filepath +
";Extended Properties='Excel 8.0;IMEX=1'"
;
//string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " + filepath + ";Extended Properties='Excel 12.0; HDR=Yes; IMEX=1'";
System.Data.OleDb.OleDbConnection Conn =
new
System.Data.OleDb.OleDbConnection(strCon);
string
strCom =
"SELECT * FROM [Sheet1$]"
;
Conn.Open();
System.Data.OleDb.OleDbDataAdapter myCommand =
new
System.Data.OleDb.OleDbDataAdapter(strCom, Conn);
DataSet ds =
new
DataSet();
myCommand.Fill(ds,
"[Sheet1$]"
);
dataGridView1.DataSource = ds.Tables[0];
Conn.Close();
return
ds;
}
private
void
button2_Click(
object
sender, EventArgs e)
{
if
(textBox1.Text ==
""
)
{
MessageBox.Show(
"请选择要导入的Excel文档!"
,
"系统提示"
, MessageBoxButtons.OK, MessageBoxIcon.Information);
return
;
}
string
filepath = textBox1.Text;
string
strcon1 = ConfigurationManager.ConnectionStrings[
"connString"
].ToString();
SqlConnection conn =
new
SqlConnection(strcon1);
//链接数据库
conn.Open();
try
{
DataSet ds =
new
DataSet();
//取得数据集
//调用上面的函数
ds = xsldata(filepath);
int
errorcount = 0;
//记录错误信息条数
int
insertcount = 0;
//记录插入成功条数
int
updatecount = 0;
//记录更新信息条数
for
(
int
i = 0; i < ds.Tables[0].Rows.Count; i++)
{
string
carnumber = ds.Tables[0].Rows[i][0].ToString();
int
carstatus = Convert.ToInt32(ds.Tables[0].Rows[i][1].ToString());
int
cartype = Convert.ToInt32(ds.Tables[0].Rows[i][2].ToString());
string
carbrand = ds.Tables[0].Rows[i][3].ToString();
if
(carnumber !=
""
&& carstatus != 0 && cartype != 0)
{
SqlCommand selectcmd =
new
SqlCommand(
"select count(*) from CarInfo where CarNumber='"
+ carnumber +
"'"
, conn);
int
count = Convert.ToInt32(selectcmd.ExecuteScalar());
if
(count > 0)
{
updatecount++;
}
else
{
SqlCommand insertcmd =
new
SqlCommand(
"insert into CarInfo(CarNumber,CarStatusID,CarTypeID,CarBrand) values("
+
"'"
+ carnumber +
"',"
+ carstatus +
","
+ cartype +
",'"
+ carbrand +
"'"
+
")"
, conn);
insertcmd.ExecuteNonQuery();
insertcount++;
}
}
else
{
//MessageBox.Show("电子表格信息有错!");
errorcount++;
}
}
myRefresh();
MessageBox.Show(insertcount +
"条数据导入成功!"
+ updatecount +
"条数据重复!"
+ errorcount +
"条数据部分信息为空没有导入!"
);
}
catch
(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
}
栏目列表最新更新- 如何使用OS模块中的stat方法
- Python os 模块
- seek() 方法
- python打开文件实例1
- Python写入文件
- 什么是流?
- 文件操作如何进制逐行读取
- Python相对路径
- with创建临时运行环境
- Python文件操作
- .Net Standard(.Net Core)实现获取配置信息
- Linux PXE + Kickstart 自动装机
- Shell 编程 基础
- Shell 编程 条件语句
- CentOS8-网卡配置及详解
- Linux中LVM逻辑卷管理
- 1.数码相框-相框框架分析(1)
- Ubuntu armhf 版本国内源
- Linux中raid磁盘阵列
- 搭建简易网站
- access教程之Access简介
- mysql 安装了最新版本8.x版本后的报错:
- Mysql空间数据&空间索引(spatial)
- 如何远程连接SQL Server数据库的图文教程
- 复制SqlServer数据库的方法
- 搜索sql语句
- sql中返回参数的值
- sql中生成查询的模糊匹配字符串
- 数据定义功能
- 数据操作功能