简体   繁体   English

从URL导入CSV并存储到SQL Server表中,而无需下载文件

[英]Import CSV from URL and store into SQL Server table without downloading the file

I've implemented a web and Windows application which hit an URL containing path to the csv file. 我已经实现了一个Web和Windows应用程序,该应用程序命中了一个包含csv文件路径的URL。 I'm using WebClient to download the file and then read data from .csv and write into a SQL Server table. 我正在使用WebClient下载文件,然后从.csv读取数据并写入SQL Server表。

Is there a way to do this without downloading/saving the .csv and writing the response .csv of the URL into the SQL Server table using a stored procedure? 有没有一种方法,而无需下载/保存.csv并使用存储过程将URL的响应.csv写入SQL Server表中呢?

Below is snippet of my working C# code. 以下是我的C#代码片段。

URL_VALUE = "http://test.url.com/table.csv?test.csv";
string csvPath = "~C:\\SaveFile\\test.csv";

WebClient client = new WebClient();
client.DownloadFile(@URL_VALUE, csvPath);

writeToSQL(csvPath, Symbol);

// Code snippet for SQL Write operation
public void writeToSQL(string url, string filepath)
{
    using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(connection))
    {
        // Set the database table name
        sqlBulkCopy.DestinationTableName = "dbo.csv_PopulateData";

        sqlBulkCopy.ColumnMappings.Add("Col1", "Val1");
        sqlBulkCopy.ColumnMappings.Add("Col2", "Val2");
        sqlBulkCopy.ColumnMappings.Add("Col3", "Val3");

        sqlBulkCopy.WriteToServer(table);
    }
}

Using HttpWebRequest and HttpWebResponse methods I was able to pull data from a .csv file present on URL and write into a SQL Server table. 使用HttpWebRequestHttpWebResponse方法,我能够从URL上存在的.csv文件中提取数据并写入SQL Server表中。

public void getFileData()
    {
        try
        {

            Uri CSVUri = new Uri("URL");
            WebRequest = (HttpWebRequest)HttpWebRequest.Create(CSVUri);
            webResponse = (HttpWebResponse)WebRequest.GetResponse();
            if (webResponse.StatusCode == HttpStatusCode.OK)
            {
                System.IO.StreamReader strReader = new System.IO.StreamReader(WebRequest.GetResponse().GetResponseStream());
                //create datatable with column names (Headers)
                Createtable(strReader.ReadLine());
                String SingleLine;
                while ((SingleLine = strReader.ReadLine()) != null)
                {
                    AddRowtable(SingleLine); //Add data into dataset
                }

                GridView1.DataSource = datatable1;

                using (SqlBulkCopy bulkCopy = new SqlBulkCopy(connection))
                {
                    foreach (DataColumn c in datatable1.Columns)
                    bulkCopy.ColumnMappings.Add(c.ColumnName, c.ColumnName);

                    bulkCopy.DestinationTableName = "dbo.tableName";
                    try
                    {
                        bulkCopy.WriteToServer(datatable1);
                    }
                    catch (Exception ex)
                    {
                        string display = "Exception Occured";
                        ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + display + "');", true);
                    }
                }

                if (strReader != null) strReader.Close();
            }
        }
        catch (System.Net.WebException ex)
        {
            string display = "Exception Occured";
            ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + display + "');", true);
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM