-1

我编写了这个 SQL 语句并使用 SQL Server Management Studio 对其进行了测试。但是我不确定如何用 C# 来做到这一点。我试过了SqlCommand.ExecuteNonQuery();,但没有用。我错过了什么?

C#:

string query = "SELECT ISNULL(CAST(CustId AS INT), 0) AS AccountNumber, 
                       ISNULL(CAST(CompanyName AS NVARCHAR),'') AS Company,
                       ISNULL(CAST(CustName AS NVARCHAR),'') AS FirstName, 
                       ISNULL(CAST(LastName AS NVARCHAR),'') AS LastName, 
                       ISNULL(CAST(Email AS NVARCHAR),'') AS EmailAddress,  
                       ISNULL(CAST(ROUND(CustLoyaltyPoints,0),0) AS NVARCHAR) AS CustomText1 
                INTO db2.dbo.CustomerTemp 
                FROM db1.dbo.Customer;";

connection = new SqlConnection(strConnect); //connection already defined above
SqlCommand command = new SqlCommand(query, connection); 

command.ExecuteNonQuery();
4

1 回答 1

0

您需要在您之前添加此行ExecuteNonQuery

connection.Open();

所以代码应该是这样的:

connection = new SqlConnection(strConnect); //connection already defined above
connection.Open();

SqlCommand command = new SqlCommand(query, connection); 
command.ExecuteNonQuery();

如果您仍然遇到错误,请发布引发的特定异常和消息。HTH...

于 2012-11-12T20:46:48.857 回答