我有以下代码:
con.Open();
string exportQuery = "INSERT INTO OPENROWSET(@oledbType,@filepath,@query)";
string filepath = "'Excel 12.0;Database=C:\\Temp\\TestExcel.xlsx;";
string oledbType = "Microsoft.ACE.OLEDB.12.0";
string query = "SELECT * FROM categoryData";
SqlCommand cmd = new SqlCommand(exportQuery, con);
cmd.Parameters.AddWithValue("@filepath", filepath);
cmd.Parameters.AddWithValue("@oledbType", oledbType);
cmd.Parameters.AddWithValue("@query", query);
cmd.ExecuteNonQuery();
con.Close();
我收到错误:
System.Data.SqlClient.SqlException:' @ oledbType'附近的语法不正确。'
所以我认为
的语法string exportQuery = "INSERT INTO OPENROWSET(@oledbType,@filepath,@query)";
不正确。有什么想法吗?
我使用上一个查询的更高级版本设法在SQL Server中使用
如果文件存在,则以下代码正确导出数据:
INSERT INTO OpenRowSet(
'Microsoft.ACE.OLEDB.12.0'
, 'Excel 12.0;Database=M:\TestExcel.xlsx;'
, 'SELECT * FROM [Sheet1$]'
)
SELECT category as Category, SUM(ButtonClick) as Count FROM
( SELECT COUNT(id) as id, category as category, locationName as location, sum(counter) as ButtonClick FROM categoryData AS t
WHERE locationName IN ('all', 'loc1', 'loc2')
AND date BETWEEN '2018-04-03' AND '2018-04-04'
GROUP BY locationName, category)
AS SUBQUERY GROUP BY category ORDER BY Count ASC
导出到C#我收到错误:
string exportQuery = @"INSERT INTO OpenRowSet('Microsoft.ACE.OLEDB.12.0', 'Excel 12.0;Database=M:\TestExcel.xlsx;' , 'SELECT * FROM [Sheet1$]')
SELECT category as Category, SUM(ButtonClick) as Count FROM
( SELECT COUNT(id) as id, category as category, locationName as location, sum(counter) as ButtonClick FROM categoryData AS t
WHERE locationName IN ('all', 'loc1', 'loc2')
AND date BETWEEN '2018-04-03' AND '2018-04-04'
GROUP BY locationName, category)
AS SUBQUERY GROUP BY category ORDER BY Count ASC";
SqlCommand cmd = new SqlCommand(exportQuery, con);
cmd.ExecuteNonQuery();
OLE DB提供程序" Microsoft.ACE.OLEDB.12.0"用于链接服务器 "(空)"报告错误。提供商没有提供任何信息 关于错误。无法初始化OLE DB的数据源对象 提供商" Microsoft.ACE.OLEDB.12.0"对于链接服务器"(null)"。'
真的很烦人的错误,并在这个问题上摸不着头脑。 :( :(
答案 0 :(得分:1)
@Praveen声明您不能使用表达式,因此必须使用动态SQL:https://stackoverflow.com/a/13831792/361842
根据以上所述,我相信这就是你所需要的:
select P17192142_BORROWER.ABN_BOR_NO, P17192142_BORROWER.ABN_BOR_NAME, ABN_ACCESS_NO, ABN_LOAN_DATE
from P17192142_BORROWER LEFT OUTER JOIN P17192142_LOAN ON
P17192142_BORROWER.ABN_BOR_NO = P17192142_LOAN.ABN_BOR_NO
where ABN_TOWN='Leicester' or ABN_TOWN='Hinkley'
ORDER BY ABN_BOR_NAME, ABN_LOAN_DATE DESC
以上生成并执行动态SQL:
string filepath = @"Excel 12.0;Database=C:\Temp\TestExcel.xlsx;";
string oledbType = "Microsoft.ACE.OLEDB.12.0";
string querySheet = "SELECT * FROM [Sheet1$]"; //amend to match the sheet name: https://stackoverflow.com/a/910199/361842
string query = "SELECT * FROM categoryData"; //this is the SQL to get the data from your SQL DB to send to your sheet.
string exportQuery = @"
declare @sql nvarchar(max) = '
INSERT INTO OpenRowSet(
' + quotename(@oledbType,'''') + '
, ' + quotename(@filepath,'''') + '
, ' + quotename(@querySheet,'''') + '
)' +
@query + ';'
exec (@sql)
";
SqlCommand cmd = new SqlCommand(exportQuery, con);
cmd.Parameters.AddWithValue("@filepath", filepath);
cmd.Parameters.AddWithValue("@oledbType", oledbType);
cmd.Parameters.AddWithValue("@querySheet", querySheet);
cmd.Parameters.AddWithValue("@query", query);
cmd.ExecuteNonQuery();
con.Close();
(这不是我自己玩过的功能,因此无法确认这是否有效;只是在本网站的其他地方找到答案)。
<强>更新强>
早些时候我说过
@Stephan声明要更新数据,您应该使用
INSERT INTO OpenRowSet( 'Microsoft.ACE.OLEDB.12.0' , 'Excel 12.0;Database=C:\Temp\TestExcel.xlsx;' , 'SELECT * FROM [Sheet1$]' ) SELECT * FROM categoryData;
代替OpenDataSource
:https://stackoverflow.com/a/29078719/361842
我已经看到情况并非如此。即见https://stackoverflow.com/a/13891752/361842的@AleksandrFedorenko。因此,修改了以上内容。