Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将数据类型从datagrid转换为datatable时出错_C#_.net - Fatal编程技术网

C# 将数据类型从datagrid转换为datatable时出错

C# 将数据类型从datagrid转换为datatable时出错,c#,.net,C#,.net,net初学者:我正在使用Datagrid显示excel工作表,当有空记录时,我会收到此错误消息 “System.FormatException:'字符串未被识别为有效的日期时间。”“ 代码: for (int i = 0; i < GridView1.Rows.Count - 1; i++) { command.Parameters.AddWithValue("@Name", GridView1.Rows[i].Cells[0].Text); co

net初学者:我正在使用Datagrid显示excel工作表,当有空记录时,我会收到此错误消息 “System.FormatException:'字符串未被识别为有效的日期时间。”“

代码:

for (int i = 0; i < GridView1.Rows.Count - 1; i++)
    {

        command.Parameters.AddWithValue("@Name", GridView1.Rows[i].Cells[0].Text);
        command.Parameters.AddWithValue("@Day_of_the_Week", Convert.ToDateTime(GridView1.Rows[i].Cells[1].Text));
        command.Parameters.AddWithValue("@Hours_Total", GridView1.Rows[i].Cells[2].Text);
        command.ExecuteNonQuery();
        command.Parameters.Clear();
    }
for(int i=0;icommand.Parameters.AddWithValue(“@Day\u of the\u Week”,Convert.ToDateTime(GridView1.Rows[i].Cells[1].Text));
command.Parameters.AddWithValue(“@Hours\u Total”,GridView1.Rows[i]。Cells[2]。Text);
command.ExecuteNonQuery();
command.Parameters.Clear();
}

这会检查您试图检索的单元格内容是否为有效的日期时间,如果是,则会转换,否则会从excel或….返回原始信息

DateTime outDate;
    for (int i = 0; i < GridView1.Rows.Count - 1; i++)
    {

        command.Parameters.AddWithValue("@Name", GridView1.Rows[i].Cells[0].Text);
        command.Parameters.AddWithValue("@Day_of_the_Week", DateTime.TryParse(GridView1.Rows[i].Cells[1].Text, out outDate) ? Convert.ToDateTime(GridView1.Rows[i].Cells[1].Text) : GridView1.Rows[i].Cells[1].Text);
        command.Parameters.AddWithValue("@Hours_Total",  GridView1.Rows[i].Cells[2].Text);
        command.ExecuteNonQuery();
        command.Parameters.Clear();
    }
DateTime过期;
对于(int i=0;icommand.Parameters.AddWithValue(“@Day\u of_the_Week”,DateTime.TryParse(GridView1.Rows[i]。单元格[1]。文本,过期)?Convert.ToDateTime(GridView1.Rows[i]。单元格[1]。文本):GridView1.Rows[i]。单元格[1]。文本);
command.Parameters.AddWithValue(“@Hours\u Total”,GridView1.Rows[i]。Cells[2]。Text);
command.ExecuteNonQuery();
command.Parameters.Clear();
}
…返回日期时间最小日期如果您的@Day_of_周参数为datetime类型:

DateTime outDate;
    for (int i = 0; i < GridView1.Rows.Count - 1; i++)
    {

        command.Parameters.AddWithValue("@Name", GridView1.Rows[i].Cells[0].Text);
        command.Parameters.AddWithValue("@Day_of_the_Week", DateTime.TryParse(GridView1.Rows[i].Cells[1].Text, out outDate) ? Convert.ToDateTime(GridView1.Rows[i].Cells[1].Text) : DateTime.MinValue);
        command.Parameters.AddWithValue("@Hours_Total",  GridView1.Rows[i].Cells[2].Text);
        command.ExecuteNonQuery();
        command.Parameters.Clear();
    }
DateTime过期;
对于(int i=0;icommand.Parameters.AddWithValue(“@Day\u of_the_Week”,DateTime.TryParse(GridView1.Rows[i]。单元格[1]。文本,过期)?Convert.ToDateTime(GridView1.Rows[i]。单元格[1]。文本):DateTime.MinValue);
command.Parameters.AddWithValue(“@Hours\u Total”,GridView1.Rows[i]。Cells[2]。Text);
command.ExecuteNonQuery();
command.Parameters.Clear();
}

您应该尝试转换输入字段,并使用转换设置参数。如果转换失败,您应该为字段使用预定义的值,或者将其设置为DBNull.value(如果数据库可以为该字段接受null)

DateTime minValue=newdatetime(1900,1,1);//对于缺少的值,可以使用任意值
Add(“@Name”,SqlDbType.NVarChar);
Add(“@Day\u of thew”,SqlDbType.DateTime);
command.Parameters.AddWithValue(“@Hours\u Total”,SqlDbType.NVarChar);
对于(int i=0;iif(!DateTime.TryParse(GridView1.Rows[i].Cells[1].Text,out day)
日=最小值;
command.Parameters[“@Name”].Value=GridView1.Rows[i]。单元格[0]。文本;
command.Parameters[“@Day\u of_the_Week”]。Value=Day;
command.Parameters[“@Hours_Total”].Value=GridView1.Rows[i]。Cells[2]。Text;
command.ExecuteNonQuery();
}
还请注意,我没有使用AddWithValue。这是一种方便的方法,但必须注意。Add方法允许您精确指定数据类型,并且此数据类型应与您的数据库类型匹配。

最后,所有参数都可以在循环外部声明,只需在循环内部设置值(无需在每个循环中重新创建它们)

让自己成为一个辅助函数,一个线性函数,它将执行一个小检查(如果知道它可以为NULL,则为NULL),然后决定要作为默认值提供什么“普通基元类型”(意思是CLR),一个用于SQL类型。为了安全起见,您还可以覆盖可为null的变量


在您的情况下,由于您正在执行UI,您可能需要第三种显示字符串默认值。如果DateTime缺少某些内容,则相应的默认值可能只是空字符串或类似“not given”的短单词“。这方面在很大程度上取决于给定UI的用途。

我不清楚您试图在“周中的某一天”(即工作日、数字索引、完整日期)中放置的数据类型是什么

但是,如果只是一个完整的日期时间,您可以使用字符串语法(如“YYYYMMDD”)将其追加到SQL中:

DateTime过期;
IFormatProvider culture=new CultureInfo(“en-US”,true)
对于(int i=0;icommand.Parameters.AddWithValue(“@Day\u of the\u Week”,Datetime.ParseExact(GridView1.Rows[i].Cells[1].Text,“yyyy-MM-dd”,区域性);
command.Parameters.AddWithValue(“@Hours\u Total”,GridView1.Rows[i]。Cells[2]。Text);
command.ExecuteNonQuery();
command.Parameters.Clear();
}

在循环中设置断点,告诉我们
GridView1.Rows[i].Cells[1].Text的值,并告诉我们如果
GridView1.Rows[i].Cells[1].Text
不是日期时间,你想用无效的格式解析日期,你能展示一下什么是
GridView1.Rows[i].Cells[1].Text
contain?在设置参数之前添加一个检查。用参数@Day\u of the_week填充的字段的数据类型是什么?GridView1.Rows[i]。Cells[1].Text从excel工作表中获取Datetime。如果有记录,则代码工作正常。如果记录为空,我将收到此错误消息。数据库中的数据类型为date。感谢您的回复。我现在收到此错误。“无法确定条件表达式的类型,因为'System.DateTime'和'string'之间没有隐式转换。”第一个示例永远不会编译,因为三元运算符的两部分应返回相同的类型。无论如何,您可以在运算符的true部分使用out变量。最后不要使用AddWithValue(特别是日期)谢谢你,史蒂夫。工作就像一个charmits时间表计算,一周中的一天是fulldate。谢谢你的回复
DateTime minValue = new DateTime(1900,1,1); // Arbitrary for missing values 
command.Parameters.Add("@Name", SqlDbType.NVarChar);
command.Parameters.Add("@Day_of_the_Week", SqlDbType.DateTime);
command.Parameters.AddWithValue("@Hours_Total", SqlDbType.NVarChar);
for (int i = 0; i < GridView1.Rows.Count - 1; i++)
{
    DateTime day;
    if(!DateTime.TryParse(GridView1.Rows[i].Cells[1].Text, out day)
       day = minValue;

    command.Parameters["@Name"].Value = GridView1.Rows[i].Cells[0].Text;
    command.Parameters["@Day_of_the_Week"].Value = day;
    command.Parameters["@Hours_Total"].Value = GridView1.Rows[i].Cells[2].Text;
    command.ExecuteNonQuery();
}
DateTime outDate;
IFormatProvider culture = new CultureInfo("en-US", true)

    for (int i = 0; i < GridView1.Rows.Count - 1; i++)
    {

        command.Parameters.AddWithValue("@Name", GridView1.Rows[i].Cells[0].Text);
        command.Parameters.AddWithValue("@Day_of_the_Week",  Datetime.ParseExact(GridView1.Rows[i].Cells[1].Text, "yyyy-MM-dd", culture);
        command.Parameters.AddWithValue("@Hours_Total",  GridView1.Rows[i].Cells[2].Text);
        command.ExecuteNonQuery();
        command.Parameters.Clear();
    }