0

我似乎得到了一个例外,上面写着“'00'附近的语法不正确。字符串',False)'后面的未闭合引号。” 我究竟做错了什么?我很难发现这样的小错误。任何帮助,将不胜感激。先感谢您。

更详细一点:如果这有助于缩小问题范围,我正在使用 Visual Studio 和 SQL Server?

SqlConnection conn = Database.GetConnection();
        SqlCommand command ;
        SqlDataAdapter adpter = new SqlDataAdapter();
        DataSet ds = new DataSet();
        XmlReader xmlFile ;
        string sql = null;



        int PatientNo=0;
       bool FurtherVisitRequired;
        string AdvisoryNotes=null;
        string Prescription=null;
            string TreatmentProvided=null;
DateTime ActualVisitDateTime;
        string Priority=null;
        DateTime ScheduledDateTime;
        string TreatmentInstructions=null;
        int MedicalStaffID;
        string VisitRefNo=null;


        //conn = new SqlConnection(conn);

        xmlFile = XmlReader.Create("\\HomeCareVisit.xml", new XmlReaderSettings());
        ds.ReadXml(xmlFile);
        int i = 0;
        conn.Open();
        for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
        {
            VisitRefNo=ds.Tables[0].Rows[i].ItemArray[0].ToString();
            PatientNo= Convert.ToInt32(ds.Tables[0].Rows[i].ItemArray[1]);
 ScheduledDateTime = Convert.ToDateTime(ds.Tables[0].Rows[i].ItemArray[2]);
 TreatmentInstructions = ds.Tables[0].Rows[i].ItemArray[3].ToString();
  MedicalStaffID= Convert.ToInt32(ds.Tables[0].Rows[i].ItemArray[4]);
            Priority = ds.Tables[0].Rows[i].ItemArray[5].ToString();
                  ActualVisitDateTime = Convert.ToDateTime(ds.Tables[0].Rows[i].ItemArray[6]);
 TreatmentProvided = ds.Tables[0].Rows[i].ItemArray[7].ToString();
            Prescription = ds.Tables[0].Rows[i].ItemArray[8].ToString();


            AdvisoryNotes = ds.Tables[0].Rows[i].ItemArray[9].ToString();





           FurtherVisitRequired =Convert.ToBoolean(ds.Tables[0].Rows[i].ItemArray[10]);








            sql = "insert into HomeCareVisit values(" + VisitRefNo + ",'" + PatientNo + "'," + ScheduledDateTime + "" + TreatmentInstructions + ",'" + MedicalStaffID + "'," + Priority+ "'," + ActualVisitDateTime + ",'" + TreatmentProvided + "'," + Prescription+ "',"+AdvisoryNotes +"',"+FurtherVisitRequired+" )";
            command = new SqlCommand(sql, conn);
           adpter.InsertCommand = command;
            adpter.InsertCommand.ExecuteNonQuery();
        }
        conn.Close();
        MessageBox.Show("Done .. ");

Icemand 回答了最初的问题。但他的回答带来了新的答案。如何在 SQL 命令中打开和关闭身份插入?

4

2 回答 2

0

您在以下行中遗漏了一些引号和逗号:

sql = "insert into HomeCareVisit values(" + VisitRefNo + ",'" + PatientNo + "'," + cheduledDateTime + "" + TreatmentInstructions + ",'" + MedicalStaffID + "'," + Priority+ "'," + ActualVisitDateTime + ",'" + TreatmentProvided + "'," + Prescription+ "',"+AdvisoryNotes +"',"+FurtherVisitRequired+" )";

缺少的引号围绕着 ScheduledDateTime、Perescription、AdvisoryNotes、TreatmentInstructions 等值。您应该在字符串和日期时间变量周围加上引号,并且不应该为数字加上引号。正确的代码应该是:

sql = "insert into HomeCareVisit values(" + VisitRefNo + ",'" + PatientNo + "','" + ScheduledDateTime + "','" + TreatmentInstructions + "','" + MedicalStaffID + "','" + Priority+ "','" + ActualVisitDateTime + "','" + TreatmentProvided + "','" + Prescription+ "','"+AdvisoryNotes +"',"+FurtherVisitRequired+" )";

我建议您检查您的变量类型并在提到的变量周围加上标记+查看每个逗号是否在正确的位置或丢失。要进行仔细检查,在将查询字符串发送到有助于调试代码的数据库之前打印出查询字符串。

于 2013-08-20T09:30:43.813 回答
0

您没有使用参数,也没有替换包含字符串 delimiter 的值"'"

您应该替换字符串中的撇号,例如TreatmentProvided.Replace("'", "''").
你不应该首先建立一个字符串。
使用参数。

PS:你可以让它使用这样的身份:

SET IDENTITY_INSERT [dbo].[HomeCareVisit] ON
-- insert here:
insert into HomeCareVisit VALUES (" + VisitRefNo + ",'" + PatientNo + "','" + ScheduledDateTime + "','" + TreatmentInstructions + "','" + MedicalStaffID + "','" + Priority+ "','" + ActualVisitDateTime + "','" + TreatmentProvided + "','" + Prescription+ "','"+AdvisoryNotes +"',"+FurtherVisitRequired+" )

-- end insert
SET IDENTITY_INSERT [dbo].[HomeCareVisit] OFF

此外,如果要将表内容写回数据库,则可以将 xml 读入数据表,然后使用 BulkInsert。

于 2013-08-20T09:26:02.340 回答