| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1345 人关注过本帖
标题:如何使用C#编程,使EXECL导入SQL中----急~
只看楼主 加入收藏
zhanwenfu
Rank: 1
等 级:新手上路
帖 子:55
专家分:0
注 册:2008-1-15
收藏
 问题点数:0 回复次数:5 
如何使用C#编程,使EXECL导入SQL中----急~
各位高手帮帮我呀~~~
搜索更多相关主题的帖子: SQL 中----急 EXECL 
2008-02-07 15:58
zhanwenfu
Rank: 1
等 级:新手上路
帖 子:55
专家分:0
注 册:2008-1-15
收藏
得分:0 
快快帮帮忙呀`~各位大哥~~
2008-02-16 12:35
liyachi
Rank: 1
等 级:新手上路
帖 子:70
专家分:0
注 册:2006-10-31
收藏
得分:0 
public void btn_DuQu_Click(object sender,System.EventArgs e)
        {//读取excel
            if(this.Up_DuQu.Value!="")
            {
                string sPath=this.Up_DuQu.Value.ToString().Trim();
                string connStr ="Provider=Microsoft.Jet.OLEDB.4.0; Data Source="+ sPath +";Extended Properties='Excel 8.0;IMEX=1'";
                string sqlStr="select * from [Sheet1$]";
                OleDbConnection myConn=new OleDbConnection(connStr);
                myConn.Open();
                OleDbCommand myCmd=new OleDbCommand(sqlStr,myConn);
                OleDbDataAdapter myda=new OleDbDataAdapter(myCmd);
                DataSet myDs=new DataSet();
                myda.Fill(myDs,"userinfo");
                DG_DaoRu.DataSource=myDs;
                DG_DaoRu.DataBind();

                if(myDs.Tables[0].Rows.Count> 0)   
                { //excel的索引从0开始
                    string sSql= "";   
                    string CnnString="Provider=SQLOLEDB;database=WeiQuan;server=(local);uid=sa;pwd=admin";   
                    OleDbConnection conn = new  OleDbConnection(CnnString);   
                    conn.Open   ();  
                    for(int i=0; i<myDs.Tables[0].Rows.Count; i++)   
                    {   
                        //ZuoPinClass_ID Site_Name Site_URL Site_IP Site_IP_City LinkDesc LinkSourc PlatForm Site_City Site_ICP Site_WLink_Man Site_WLink_Tel Site_WLink_MTel Site_WLink_Email Site_WLink_QQ Site_WLink_MSN Site_WLink_CZ Site_Link_Man Site_Link_Tel  Site_Link_Email Site_Link_QQ Site_Link_MSN Site_Link_CZ Site_Abstract
                        string sSite_Contact=myDs.Tables[0].Rows[i].ItemArray[14].ToString();//电话、Email、传真(WQ_Main)
                        string[] sitetel=sSite_Contact.Split('$');
                        string site0=sitetel[0];
                        string site1=sitetel[1];
                        string site2=sitetel[2];
                        string site3=sitetel[3];
                        string site4=sitetel[4];
                        string site5=sitetel[5];
                        sSql="insert into WQ_Main(ZuoPinClass_ID,Site_Name,Site_URL,Site_IP,Site_IP_City,LinkDesc,LinkSourc,PlatForm,Site_City,Site_ICP,Site_Link_Man,Site_Link_Tel,Site_Link_Email,Site_Link_QQ,Site_Link_MSN,Site_Link_CZ,Site_Abstract) values ('15', '"+myDs.Tables[0].Rows[i].ItemArray[2].ToString()+"','"+myDs.Tables[0].Rows[i].ItemArray[3].ToString()+"','"+myDs.Tables[0].Rows[i].ItemArray[4].ToString()+"','"+myDs.Tables[0].Rows[i].ItemArray[5].ToString()+"','"+myDs.Tables[0].Rows[i].ItemArray[6].ToString()+"','"+myDs.Tables[0].Rows[i].ItemArray[7].ToString()+"','"+myDs.Tables[0].Rows[i].ItemArray[8].ToString()+"','"+myDs.Tables[0].Rows[i].ItemArray[9].ToString()+"','"+myDs.Tables[0].Rows[i].ItemArray[13].ToString()+"','"+site0+"','"+site1+"','"+site2+"','"+site3+"','"+site4+"','"+site5+"','"+myDs.Tables[0].Rows[i].ItemArray[16].ToString()+"');insert into WQ_FuDong(ZuoPinClass_ID,Site_Name,Site_URL,Qin_URL) values ('15', '"+myDs.Tables[0].Rows[i].ItemArray[2].ToString()+"','"+myDs.Tables[0].Rows[i].ItemArray[3].ToString()+"','"+myDs.Tables[0].Rows[i].ItemArray[27].ToString()+"')";   
                        //浮动 sSql="insert into WQ_FuDong(ZuoPinClass_ID,Site_Name,Site_URL,Qin_URL) values ('"+myDs.Tables[0].Rows[i].ItemArray[1].ToString()+"', '"+myDs.Tables[0].Rows[i].ItemArray[2].ToString()+"','"+myDs.Tables[0].Rows[i].ItemArray[3].ToString()+"','"+myDs.Tables[0].Rows[i].ItemArray[27].ToString()+"')";   
                        try   
                        {   
                            OleDbCommand my_Cmd=new   OleDbCommand(sSql,conn);   
                            my_Cmd.ExecuteNonQuery();   
                            lbl_msg.Text = "数据导入成功!!";  
                        }   
                        catch   
                        {   
                            lbl_msg.Text ="数据导入失败";   
                        }   
                    }   
                    conn.Close();   
                }   
   
               
                else
                {
                    this.lbl_msg.Text="请选择EXCEL文件!!!";
                    return;
                }
            }
        }
2008-02-20 08:54
chlkzs
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2008-2-20
收藏
得分:0 
回复
代码比较详细,收益非浅,谢谢
2008-02-20 10:31
smoon
Rank: 1
等 级:禁止访问
帖 子:461
专家分:0
注 册:2006-4-29
收藏
得分:0 
的确不错!
2008-02-20 20:08
623860308
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2008-1-2
收藏
得分:0 
太费事 sql 2000 就自带这个功能
2008-02-20 20:22
快速回复:如何使用C#编程,使EXECL导入SQL中----急~
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.022576 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved