好的,所以从我之前的问题来看,这是我如何从 SQL Server 中提取数据然后将相同的数据插入到 CtreeACE 中的一个迭代,其中已经为要存储在其中的值设置了表。当我运行我得到的代码时
我尝试过的事情:
- 重写方法
- 优化物品被拉出后的存储方式
- 确保名称与正确的呼叫名称匹配
- 多次调试我的代码,但仍然无法理解它
这是代码
class Program
{
static CtreeSqlConnection _conn;
static CtreeSqlCommand cmd;
static CtreeSqlDataReader reader;
static void Main(string[] args)
{ //Creating the connection to the SQL server
SqlConnection conn = new SqlConnection("Server=bldg-db-pri.MDHUN.us.ups.com\\p001;Database=D90;Integrated Security=true");
//Open the connection
conn.Open();
SqlCommand cmd = new SqlCommand("SELECT TOP(100) l.tracingID, u.Sch_dt, p.address, p.city, p.state, u.zip, m.Time " +
"FROM D490AD0.dbo.TUWUOW1 u WITH (nolock) " +
"INNER JOIN D90.dbo.TUW p WITH (nolock) ON p.UOW = u.UOW " +
"INNER JOIN D90.dbo.TUW2 l WITH (nolock) ON l.UOW = u.UOW " +
"CROSS JOIN D90.dbo.tTN m " +
"WHERE " +
"u.Sch_dt = '2018-07-23' ", conn); //query that intializes after the connection is opened
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
Console.WriteLine($"{reader["tracingID"]} |" +
$"{reader["Time"]} |" +
$"{reader["state"]} |" +
$"{reader["address1"]} |" +
$"{reader["address1"]} |" +
$"{reader["address3"]} |" +
$"{reader["city"]} |" +
$"{reader["zip"]} |" +
$"{reader["Sch_dt"]}"
);
try
{
DataInsertion($"{reader["tracingID"]} " ,
$"{reader["Time"]} " ,
$"{reader["state"]} " ,
$"{reader["address1"]} ",
$"{reader["address1"]} " ,
$"{reader["address3"]} ",
$"{reader["city"]} " ,
$"{reader["zip"]} " ,
$"{reader["Sch_dt"]}"
);
}
catch (CtreeSqlException e)
{
Console.WriteLine(e + " couldn't run method");
}
}
}
reader.Close();
conn.Close();
if(Debugger.IsAttached)
{
Console.ReadLine();
}
}
public static void DataInsertion(string tracingID, string Time, string state, string address1, string address2, string address3, string city, string zip, string Sch_dt)
{
_conn = new CtreeSqlConnection();
_conn.ConnectionString = "UID=ADMIN;PWD=ADMIN;Database=AttributeDB;Server=localhost;Service=6597;";
_conn.Open();
if (_conn == null)
{
Console.WriteLine("Could not connect to Ctree");
}
try
{
cmd.CommandText = "INSERT INTO tbl6(tracingID, Time, state, address, city, zipcode, dates) VALUES(tracingID, Time, state ,address, city, zip, Sch_dt)";
cmd.Parameters.Clear();
cmd.Parameters.Add(new CtreeSqlParameter(tracingID));
cmd.Parameters.Add(new CtreeSqlParameter(Time));
cmd.Parameters.Add(new CtreeSqlParameter(state));
cmd.Parameters.Add(new CtreeSqlParameter(address));
cmd.Parameters.Add(new CtreeSqlParameter(city));
cmd.Parameters.Add(new CtreeSqlParameter(zip));
cmd.Parameters.Add(new CtreeSqlParameter(Sch_dt));
cmd.ExecuteNonQuery();
}
catch (CtreeSqlException ctsqlEx)
{
Console.WriteLine("Something went wrong with the command script");
}
}
}
这是该方法将提取的数据插入 Ctreedatabase 的位置
CtreeSqlCommand cmd = new CtreeSqlCommand("INSERT INTO tbl6(tracingID, Time, state, address, city, zipcode, dates) VALUES(tracingID, Time, state ,address, city, zip, Sch_dt)", _conn);
cmd.Parameters.Clear();
cmd.Parameters.Add(new CtreeSqlParameter(tracingID));
cmd.Parameters.Add(new CtreeSqlParameter(Time));
cmd.Parameters.Add(new CtreeSqlParameter(state));
cmd.Parameters.Add(new CtreeSqlParameter(address));
cmd.Parameters.Add(new CtreeSqlParameter(city));
cmd.Parameters.Add(new CtreeSqlParameter(zip));
cmd.Parameters.Add(new CtreeSqlParameter(Sch_dt));
cmd.ExecuteNonQuery();
}
我假设这些值没有被传入?每次我运行程序时,ctree 数据库中都不会显示任何值
代码运行后的异常
Ctree.Data.SqlClient.CtreeSqlException (0x7FFFB1DD): Syntax error ---> Ctree.SqlClient.Common.FcSqlException: Syntax error
at Ctree.SqlClient.FcSqlXApi.SQLExec(FcStatement stmt, Int32 InStatementType, FcSqlDA ida, FcSqlDA oda, FcSqlCA sqlca)
at Ctree.SqlClient.FcSqlXApi.Prepare(FcStatement stmt, FcSqlDA input_sqlda, FcSqlDA output_sqlda, Int32 fetchSize)
at Ctree.SqlClient.FcConnection.Prepare(FcStatement statement, FcSqlDA inputDA, FcSqlDA outputDA, Int32 fetchSize)
at Ctree.SqlClient.FcPreparedStatement..ctor(FcConnection connexion, String sql, Int32 fetchSize, Int32 timeout)
at Ctree.Data.SqlClient.CtreeSqlCommand.InternalPrepare(Boolean resultSet)
at Ctree.Data.SqlClient.CtreeSqlCommand.ExecuteNonQuery()
at Ctree.Data.SqlClient.CtreeSqlCommand.ExecuteNonQuery()