即使我的DataReader
似乎正确地解释了这些值(差不多),但是它缺少一个列,没有写入文件,而在最后一行它正在抛出一个
+ base {"Invalid attempt to read when no data is present."} System.SystemException {System.InvalidOperationException}
因为它退出while sqlReader.Read()
循环。
myCommand.CommandText = "use sis_comparison_data select reg_batch_uid, user_id_reg, reg_first, reg_mid, reg_last, reg_email, reg_stu_id, registrar_row_status from sis_temp where bb_row_status is null";
string bb_users = @"C:\BB_USERS.bbd";
using (StreamWriter file = new StreamWriter(bb_users, false))
{
file.WriteLine("SYSTEM_ROLE|INSTITUTION_ROLE|EXTERNAL_PERSON_KEY|USER_ID|PASSWD|FIRSTNAME|MIDDLENAME|LASTNAME|EMAIL|STUDENT_ID|ROW_STATUS");
using (SqlDataReader sqlReader = myCommand.ExecuteReader())
{
while (sqlReader.Read())
{
file.WriteLine("none|student|", sqlReader["reg_batch_uid"].ToString() + "|" + sqlReader["user_id_reg"].ToString() + "|" + sqlReader["reg_stu_id"].ToString() + "|" + sqlReader["reg_first"].ToString() + "|" + sqlReader["reg_mid"].ToString() + "|" + sqlReader["reg_last"].ToString() + "|" + sqlReader["reg_email"].ToString() + "|" + sqlReader["reg_stu_id"].ToString() + "|" + sqlReader["registrar_row_status"].ToString());
}
sqlReader.Close();
}
}
我的代码是创建文件并编写第一行,并且在它之后只写了"none|student|"
。无论出于何种原因,它都不会拿起registrar_row_status
列。
SQL查询的工作原理是我在将所有SQL脚本编写到代码之前就已经完成了所有SQL脚本。
这是调试输出在单步执行代码时显示的内容。
+ sqlReader {System.Data.SqlClient.SqlDataReader} System.Data.SqlClient.SqlDataReader
sqlReader["reg_batch_uid"] "johndoe" object {string}
sqlReader["reg_email"] "jdoe@com.edu" object {string}
sqlReader["reg_first"] "john" object {string}
sqlReader["reg_last"] "doe" object {string}
sqlReader["reg_mid"] "" object {string}
sqlReader["reg_stu_id"] "1234567" object {string}
sqlReader["user_id_reg"] "johndoe" object {string}
如您所见,它缺少registrar_row_status
答案 0 :(得分:2)
我相信你StreamWriter.WriteLine
重载的第一个参数需要是一个格式字符串。请参阅the documentation。
答案 1 :(得分:1)
试试这个:
file.WriteLine("none|student|" + sqlReader["reg_batch_uid"].ToString() + "|" + sqlReader["user_id_reg"].ToString() + "|" + sqlReader["reg_stu_id"].ToString() + "|" + sqlReader["reg_first"].ToString() + "|" + sqlReader["reg_mid"].ToString() + "|" + sqlReader["reg_last"].ToString() + "|" + sqlReader["reg_email"].ToString() + "|" + sqlReader["reg_stu_id"].ToString() + "|" + sqlReader["registrar_row_status"].ToString());