【问题标题】:reading from datagrid writng to sql VIA xml从datagrid读取写入sql VIA xml
【发布时间】:2013-08-20 16:00:05
【问题描述】:

我正在使用此代码。

 DataTable dst = new DataTable();
        dst = (DataTable)dataGridView1.DataSource;
 this.dataGridView1.Columns[2].ValueType = typeof(DateTime);

        this.dataGridView1.Columns[6].ValueType = typeof(DateTime);
        dst.TableName = "DataGridviewTpXml";
        dst.WriteXml("\\HomeCareVisit.xml", true);
        MessageBox.Show("downloaded to file");

从 datagridview 写入 XML 和这段代码

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]);
                                       FurtherVisitRequired = Convert.ToBoolean(ds.Tables[0].Rows[i].ItemArray[10]);




                            sql = "SET IDENTITY_INSERT HomeCareVisit ON update HomeCareVisit set PatientNo='" + PatientNo + "',ScheduledDateTime='" + ScheduledDateTime + "',TreatmentInstructions='" + TreatmentInstructions + "', MedicalStaffID='" + MedicalStaffID + "', Priority='" + Priority + "',ActualVisitDateTime='" + ActualVisitDateTime + "',TreatmentProvided='" + TreatmentProvided + "',Prescription='" + Prescription + "',AdvisoryNotes='" + AdvisoryNotes + "',FurtherVisitRequired='" + (FurtherVisitRequired ? "1" : "0") + "'where VisitRefNo='" + VisitRefNo + "';";

从 xml 读取到 SQL。但是它给了我以下异常

“将 varchar 数据类型转换为 datetime 数据类型导致值超出范围。 该语句已终止。” 如何解决这个问题?

我正在尝试制作的应用程序存储某些详细信息,包括时间日期。

IDE 正在使用 Visual Studio 语言 ADO.Net c# 服务器 SQL 服务器。

任何帮助将不胜感激,谢谢。

附加细节我正在使用 datagridview 添加/编辑和删除记录。如果不清楚,我希望一些额外的信息会有所帮助?

【问题讨论】:

    标签: c# sql xml ado.net


    【解决方案1】:

    正如异常所说,您的字符串无法转换回日期时间,所以它可以在这个阶段进行

    ActualVisitDateTime = Convert.ToDateTime(ds.Tables[0].Rows[i].ItemArray[6]);
    

    或者在这一步

    ScheduledDateTime = Convert.ToDateTime(ds.Tables[0].Rows[i].ItemArray[2]);
    

    你检查过值吗??

    为什么在再次将其转换为 sql 字符串时将其转换为日期时间??

    【讨论】:

    • 当我在那个阶段将它视为字符串时,它会给我一个转换错误。
    • 因为你的属性是DateTime,试试DateTime.TryParse方法吧。但请注意,如果它无法解析,您的 sql 字符串可能会崩溃
    • “可能会掉下来”是什么意思?还有代码行会怎么走?
    • 当你构建你的 sql 字符串时,你最终可能会添加 null + 字符串,这会给你带来错误。如果要修复,请找出它无法转换为日期时间的字符串。可能看看你的数据库
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多