C#怎么把excel转到access数据库,求代码。
2个回答
展开全部
private void bind()
{
OleDbConnection conn = new OleDbConnection("provider=microsoft.jet.oledb.4.0; data source=" +Server.MapPath("../Book1.xls") + "; extended properties=excel 8.0");
DataTable dt = new DataTable();
OleDbDataAdapter da = new OleDbDataAdapter("select * from [Sheet1$]", conn);
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
private static DataTable dt;
private static OleDbDataAdapter da;
rotected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
bindgridview2();
}
private void bindgridview2()
{
dt = new DataTable();
OleDbConnection conn = new OleDbConnection("provider=microsoft.jet.oledb.4.0; data source=D:\\lx.mdb");
da = new OleDbDataAdapter("select * from lx", conn);
da.Fill(dt);
GridView2.DataSource = dt;
GridView2.DataBind();
}
protected void updat()
{
OleDbConnection conn = new OleDbConnection("provider=microsoft.jet.oledb.4.0; data source=D:\\lx.mdb");
da = new OleDbDataAdapter("select * from lx", conn);
OleDbCommand cmd = new OleDbCommand("delete * from lx where id = @id", conn);
cmd.Parameters.Add("@id", OleDbType.Integer, 4, "id");
da.DeleteCommand = cmd;
cmd = new OleDbCommand("update lx set where ", conn);
cmd.Parameters.Add("@txt", OleDbType.VarChar, 50, "txt");
cmd.Parameters.Add("@id", OleDbType.Integer, 4, "id");
da.UpdateCommand = cmd;
da.Update(dt);
}
如果编译器报错“违反并发性: UpdateCommand 影响了预期 1 条记录中的 0 条。”,有可能是以下几种情况
1.数据库中没有定义主键。
2.更新数据库的时候在datatable表中误修改了主键的值。
3.字段类型不一致或字段不一致
{
OleDbConnection conn = new OleDbConnection("provider=microsoft.jet.oledb.4.0; data source=" +Server.MapPath("../Book1.xls") + "; extended properties=excel 8.0");
DataTable dt = new DataTable();
OleDbDataAdapter da = new OleDbDataAdapter("select * from [Sheet1$]", conn);
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
private static DataTable dt;
private static OleDbDataAdapter da;
rotected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
bindgridview2();
}
private void bindgridview2()
{
dt = new DataTable();
OleDbConnection conn = new OleDbConnection("provider=microsoft.jet.oledb.4.0; data source=D:\\lx.mdb");
da = new OleDbDataAdapter("select * from lx", conn);
da.Fill(dt);
GridView2.DataSource = dt;
GridView2.DataBind();
}
protected void updat()
{
OleDbConnection conn = new OleDbConnection("provider=microsoft.jet.oledb.4.0; data source=D:\\lx.mdb");
da = new OleDbDataAdapter("select * from lx", conn);
OleDbCommand cmd = new OleDbCommand("delete * from lx where id = @id", conn);
cmd.Parameters.Add("@id", OleDbType.Integer, 4, "id");
da.DeleteCommand = cmd;
cmd = new OleDbCommand("update lx set where ", conn);
cmd.Parameters.Add("@txt", OleDbType.VarChar, 50, "txt");
cmd.Parameters.Add("@id", OleDbType.Integer, 4, "id");
da.UpdateCommand = cmd;
da.Update(dt);
}
如果编译器报错“违反并发性: UpdateCommand 影响了预期 1 条记录中的 0 条。”,有可能是以下几种情况
1.数据库中没有定义主键。
2.更新数据库的时候在datatable表中误修改了主键的值。
3.字段类型不一致或字段不一致
2012-02-14
展开全部
先从excel中取出来,然后update到access数据库中就行了。
读取excel文件:http://hi.baidu.com/hjywyj/blog/item/0e6436300798def41a4cff94.html
批量更新access数据库:http://hi.baidu.com/hjywyj/blog/item/1689937453b8ba04b051b9b5.html
读取excel文件:http://hi.baidu.com/hjywyj/blog/item/0e6436300798def41a4cff94.html
批量更新access数据库:http://hi.baidu.com/hjywyj/blog/item/1689937453b8ba04b051b9b5.html
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询