正文

从SQL数据库中导出数据到ACCESS2007-04-02 10:44:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/mimengjiangnan/24541.html

分享到:

 

 

/// <summary>

/// WebServiceDataDown 的摘要说明

/// </summary>

[WebService(Namespace = "http://tempuri.org/")]

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

public class WebServiceDataDown : System.Web.Services.WebService {

 

    public WebServiceDataDown () {

 

        //如果使用设计的组件,请取消注释以下行

        //InitializeComponent();

    }

 

    #region 导入数据到ACCESS并压缩文件

    [WebMethod]

    public void LoadDataToAccess()

    {

 

        DataSet ds = GetDataFromSQL();

        InsertDataToAccess(ds);

        WinrarFile();

    }

    #endregion

    #region 从SQL数据库里读取数据

    [WebMethod]

    protected DataSet GetDataFromSQL()

    {

        string sqlstr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

        string sql = String.Empty;

        SqlConnection con = new SqlConnection(sqlstr);

        DataSet ds = new DataSet();

        #region SYYQXX

        sql = "SELECT SYBH,YQBH,YQMC,GG,DW,MZSXSL FROM SYYQXX";

        SqlDataAdapter da_SYYQXX = new SqlDataAdapter(sql, con);

        da_SYYQXX.Fill(ds, "SYYQXX");

        #endregion

 

        return ds;

    }

    #endregion

    #region 把数据插入到ACCESS数据库

    [WebMethod]

    protected void InsertDataToAccess(DataSet ds)

    {

        string ole = String.Empty;

        string SourcePath = Server.MapPath("UpDownload\\MyDownload\\WorkData\\BaseCode.mdb");

        string oleconstr = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + SourcePath + "; User ID=admin;";

        OleDbConnection olecon = new OleDbConnection(oleconstr);

        olecon.Open();

        foreach (DataTable dt in ds.Tables)

        {

            switch (dt.TableName)

            {

 

case "":

。。。

break;

 

                #region SYYQXX

                case "SYYQXX":

                    OleDbCommand olecom_SYYQXX = new OleDbCommand("DELETE FROM SYYQXX", olecon);

                    olecom_SYYQXX.ExecuteNonQuery();

                    OleDbDataAdapter oleda_SYYQXX = new OleDbDataAdapter("SELECT SYBH,YQBH,YQMC,GG,DW,MZSXSL FROM SYYQXX", olecon);

                    DataSet oleds_SYYQXX = new DataSet();

                    oleda_SYYQXX.Fill(oleds_SYYQXX, "SYYQXX");

                    for (int i = 0; i < ds.Tables["SYYQXX"].Rows.Count; i++)

                    {

                        DataRow dr = oleds_SYYQXX.Tables[0].NewRow();

                        dr["SYBH"] = ds.Tables["SYYQXX"].Rows[i]["SYBH"];

                        dr["YQBH"] = ds.Tables["SYYQXX"].Rows[i]["YQBH"];

                        dr["YQMC"] = ds.Tables["SYYQXX"].Rows[i]["YQMC"];

                        dr["GG"] = ds.Tables["SYYQXX"].Rows[i]["GG"];

                        dr["DW"] = ds.Tables["SYYQXX"].Rows[i]["DW"];

                        dr["MZSXSL"] = ds.Tables["SYYQXX"].Rows[i]["MZSXSL"];

                        oleds_SYYQXX.Tables[0].Rows.Add(dr);

                    }

                    OleDbCommandBuilder builderSYYQXX = new OleDbCommandBuilder(oleda_SYYQXX);

                    oleda_SYYQXX.Update(oleds_SYYQXX, "SYYQXX");

                    oleds_SYYQXX.Dispose();

                    break;

                #endregion

                default :

                    break;    

            }

        }

        olecon.Close();

    }

    #endregion

    #region 压缩文件

    [WebMethod]

    protected void WinrarFile()

    {

        try

        {

            if (File.Exists(Server.MapPath("UpDownload\\MyDownload\\BaseCode.zip")))

            {

                File.Delete(Server.MapPath("UpDownload\\MyDownload\\BaseCode.zip"));

            }

            string strtxtPath = Server.MapPath("UpDownload\\MyDownload\\WorkData\\BaseCode.mdb");

            string strzipPath = Server.MapPath("UpDownload\\MyDownload\\BaseCode.zip");

            System.Diagnostics.Process Process1 = new System.Diagnostics.Process();

            Process1.StartInfo.FileName = "Winrar.exe";

            Process1.StartInfo.CreateNoWindow = true;

            Process1.StartInfo.Arguments = " a -ep " + strzipPath + " " + strtxtPath;

            Process1.Start();

            Process1.Dispose();

        }

        catch (Exception ex)

        {

            throw ex;

        }

    }

    #endregion

}

 

 

阅读(2729) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册