C#SqlDataAdapter到ODBC导出 [英] C# SqlDataAdapter to ODBC export
问题描述
大家好,
我正在尝试通过转换程序的输出部分来适应程序,以便可以像以前将其导出到纯文本一样,将其导出到ODBC(SAS).我创建一个SqlDataAdapter"da";通过执行从连接到SQL Server的查询(存储在_sqlCommand中).稍后,我用该查询的结果填充表dataTable.正如我在调试中看到的那样,该部分很好.我的下一步将是导出"将dataTable转换为具有相同结构的ODBC(SAS)表.因此,我在sbInsertCommand2中创建了另一个查询,该查询将从"dataTable"中传输记录.到我的目标表"Baseline2"(在SAS中使用的"Demo"目录中).显然,这会构成我执行最后一行时遇到的错误(可能是[dataTable]引用).
有人可以帮助我如何正确构建该查询吗?我想避免按记录导出数据集记录.
//*****这是保存结果集的表:
DataTable dataTable = new DataTable();
try
{
//*****这是您的可理解SQL数据库的数据适配器:
SqlDataAdapter da =新的SqlDataAdapter(_sqlCommand,_connection);
_connection.Open();
//*****用select语句的查询结果填充数据表:
//***** dataTable将在以后执行的查询中使用一步使用ExecuteScalar()
int recordsAffected = da.Fill(dataTable);
StringBuilder sbInsertCommand2 = new StringBuilder();
sbInsertCommand2.Append("INSERT INTO Demo.") ;
sbInsertCommand2.Append("Baseline2");
sbInsertCommand2.Append("select * from [dataTable]");
_insertCommand2 =新的OdbcCommand(sbInsertCommand2.ToString(), _sasConnection);
_insertCommand2.ExecuteScalar();
Hi all,
I am trying to adapt a program, by transforming the output part of it, so it can export to ODBC(SAS), as previously it exported to plain text.
Thus I create an SqlDataAdapter "da" by executing a query( stored in _sqlCommand) from a connection to an SQL Server.
Later I fill the table dataTable with the result from that query. That part is fine as I can see in the debug the expected result. My next step would be to "export" dataTable into an ODBC(SAS) table with the same extructure. Thus I created another query into sbInsertCommand2, which would transfer the records from "dataTable" to my target table "Baseline2"(in "Demo" directory, as used in SAS). Odviously has this construct an error(probably the [dataTable] reference) that I get when executing the last row.
Can anybody help me on how to build that query properly? I would like to avoid exporting the dataset record by record.
//***** This is your table to hold the result set:
DataTable dataTable = new DataTable();
try
{
//***** This is your data adapter that understands SQL databases:
SqlDataAdapter da = new SqlDataAdapter(_sqlCommand, _connection);
_connection.Open();
//***** Fill the data table with select statement's query results:
//***** dataTable will be used one step later in the query executed with ExecuteScalar()
int recordsAffected = da.Fill(dataTable);
StringBuilder sbInsertCommand2 = new StringBuilder();
sbInsertCommand2.Append("INSERT INTO Demo.");
sbInsertCommand2.Append("Baseline2");
sbInsertCommand2.Append(" select * from [dataTable]");
_insertCommand2 = new OdbcCommand(sbInsertCommand2.ToString(), _sasConnection);
_insertCommand2.ExecuteScalar();
推荐答案
你好圣地亚哥,
http://forums.microsoft.com/MSDN/ShowForum.aspx?ForumID=87&SiteID=1 .
这篇关于C#SqlDataAdapter到ODBC导出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!