从 URL 导入 CSV 并存储到 SQL 服务器 table 而无需下载文件
Import CSV from URL and store into SQL Server table without downloading the file
我已经实现了一个 Web 和 Windows 应用程序,它命中了一个包含 csv 文件路径的 URL。我正在使用 WebClient
下载文件,然后从 .csv
读取数据并写入 SQL 服务器 table.
没有 downloading/saving .csv
并将 URL 的响应 .csv
写入 SQL 服务器 table 使用存储过程?
下面是我的 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);
}
}
使用 HttpWebRequest
和 HttpWebResponse
方法,我能够从 URL 上存在的 .csv
文件中提取数据并写入 SQL 服务器 table.
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);
}
}
我已经实现了一个 Web 和 Windows 应用程序,它命中了一个包含 csv 文件路径的 URL。我正在使用 WebClient
下载文件,然后从 .csv
读取数据并写入 SQL 服务器 table.
没有 downloading/saving .csv
并将 URL 的响应 .csv
写入 SQL 服务器 table 使用存储过程?
下面是我的 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);
}
}
使用 HttpWebRequest
和 HttpWebResponse
方法,我能够从 URL 上存在的 .csv
文件中提取数据并写入 SQL 服务器 table.
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);
}
}