我正在準備一個Windows窗體應用程序,我想將文件下載到桌面並將其複製到數據網格。 我寫了一些代碼,但我不打包得到的結果...能有人整頓和給我的解決方案.... 代碼如下....獲取錯誤:將文件下載到特定位置,C#,winforms
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();
我我收到以下錯誤:
The ConnectionString has not be properly initialized.
您是否有例外?當你運行代碼時會發生什麼?你必須更清楚你的問題 –
你需要編輯你的問題與你得到的錯誤,並儘量不要把它添加爲圖片。 –
Hi @VenkattSwameSoma如果任何答案已解決您的問題,請考慮通過點擊複選標記來接受它。這向更廣泛的社區表明,您已經找到了解決方案,併爲答覆者和您自己提供了一些聲譽。沒有義務這樣做。 –