对于我的数据插入方法,如何检查从数据库中提取的数据的值是否正确传递或是否传递?
For my data insertion method how do I check that the values from the pulled data from the database are being passed correctly or if at all?
好的,根据我之前的问题,这是我如何从 SQL 服务器中提取数据然后将相同数据插入 CtreeACE 的迭代,其中 table 已经设置了值存放在那里。当我 运行 我得到的代码时
我尝试过的事情:
- 重写方法
- 细化项目被拉取后的存储方式
- 确保名称与正确的调用名称匹配
- 多次调试我的代码,但仍然无法理解它
这是代码
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");
}
}
}
这是方法将拉取的数据插入 Ctree 数据库的地方
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();
}
我假设没有传递值?
每次我 运行 值的程序 none 出现在 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()
你可以尝试替换:
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));
像这样:
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", tracingId));
等即对所有其他参数执行相同操作。
请注意,我将您的命令分成两行,只是为了更加明显,我在每个参数值之前添加了一个“@”,所以这只是为了便于阅读。
好的,根据我之前的问题,这是我如何从 SQL 服务器中提取数据然后将相同数据插入 CtreeACE 的迭代,其中 table 已经设置了值存放在那里。当我 运行 我得到的代码时
我尝试过的事情:
- 重写方法
- 细化项目被拉取后的存储方式
- 确保名称与正确的调用名称匹配
- 多次调试我的代码,但仍然无法理解它
这是代码
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");
}
}
}
这是方法将拉取的数据插入 Ctree 数据库的地方
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();
}
我假设没有传递值? 每次我 运行 值的程序 none 出现在 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()
你可以尝试替换:
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));
像这样:
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", tracingId));
等即对所有其他参数执行相同操作。
请注意,我将您的命令分成两行,只是为了更加明显,我在每个参数值之前添加了一个“@”,所以这只是为了便于阅读。