得到错误:将文件下载到特定位置,C#,winforms

时间:2015-12-19 10:07:00

标签: c# windows winforms file download

我正在准备一个Windows窗体应用程序,我想将一个文件下载到桌面并将其复制到数据网格。 我写了一些代码,但我并没有得到结果...可以纠正一些并向我展示解决方案.... 代码如下....

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Data.OleDb;
using System.Collections.ObjectModel;
using Microsoft.Office.Interop.Excel;



namespace market_lot_info
{
    public partial class Form1 : Form
    {
        OleDbConnection Econ;

        string constr, Query;
        string conStr = string.Empty;


        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            DirectoryInfo dir = new DirectoryInfo(@"C:\OIAtool");
            if (dir.Exists)
                dir.Delete(true);

            Directory.CreateDirectory(@"C:\OIAtool");

            WebClient wc=new WebClient();


            // download file to specified folder

            string filepath = @"C:\OIAtool\" + "fo_mktlots.csv";
            string urlmktlot = @"http://www.nseindia.com/content/fo/fo_mktlots.csv";
            Uri uri = new Uri(@"http://www.nseindia.com");
            string filename = string.Empty;


            uri = new Uri(urlmktlot);
            filename = @"C:\OIAtool\" + "fo_mktlots.csv";

            constr = string.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 12.0 Xml;HDR=YES;""", filepath);
            wc.Headers.Add("Accept: text/html, application/xhtml+xml, */*");
            wc.Headers.Add("User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)");
            wc.DownloadFile(uri, filename);


            System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0; Data Source = " + filename + "; Extended Properties = \"Excel 8.0;HDR=Yes;IMEX=1\";");
            Econ = new OleDbConnection(conStr);

            Econ.Open();

            Query = string.Format("Select * FROM " + "[" + filename + "$]");


            System.Data.OleDb.OleDbDataAdapter adapter = new System.Data.OleDb.OleDbDataAdapter(Query, conn);


            DataSet ds = new DataSet();

            Econ.Close();
            //oda.Fill(ds);
            adapter.Fill(ds);

            dataGridViewLotInfo.DataSource = ds.Tables[0];


        }

    }
}

Econ.Open();我收到以下错误:

  

ConnectionString未正确初始化。

1 个答案:

答案 0 :(得分:0)

您永远不会为conStr提供值,但您定义constr

// capital 'S'
string conStr = string.Empty;

// lower case 's'
constr = string.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 12.0 Xml;HDR=YES;""", filepath);

我猜这是无意的。 您需要将其更改为:

// capital 'S'
conStr = string.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 12.0 Xml;HDR=YES;""", filepath);