在将 DateTime 参数类型传递给 DB 时使用 String.Format。抛出异常。怎么了?

Using String.Format when passing DateTime arguments type to DB. Exception is thrown. What is wrong?

         object args = new object[] { "Project", "ProjectName", "@PrName", "ProjectStartDate", "@StartDate", "ProjectEndDate", "@EndDate", "ProjectId", "@Id" };

//******************这一行导致异常抛出 *******************

            string commandString = string.Format(@"UPDATE FROM {0} SET {1} = {2}, {3} = {4} , {5} = {6} WHERE {7} = {8}", args);

            command.CommandText = commandString;
            command.CommandType = CommandType.Text;
            command.Connection = connection;
            command.Parameters.Add("@Id", SqlDbType.Int).Value = selId;
            command.Parameters.Add("@PrName", SqlDbType.Text).Value = projectName;

            command.Parameters.Add("@StartDate", SqlDbType.DateTime).Value = startDate;
            command.Parameters.Add("@EndDate", SqlDbType.DateTime).Value = endDate;

            connection.Open();
            int result = command.ExecuteNonQuery();

// **************** 异常详情 ************************* ****

System.FormatException 未处理 Message=Index(从零开始)必须大于或等于零且小于参数列表的大小。 来源=mscorlib 堆栈跟踪: 在 System.Text.StringBuilder.AppendFormat(IFormatProvider 提供者,字符串格式,对象 [] 参数)

   at System.String.Format(IFormatProvider provider, String format, Object[] args)

   at System.String.Format(String format, Object arg0)

   at DAL.DataAccess.UpdateRec(Int32 selId, String projectName, DateTime startDate, DateTime endDate) in \Projects\Consulting\DAL\DataAccess.cs:line 106
   at Consulting.frmProject.btnAdd_Click(Object sender, EventArgs e) in \frmProject.cs:line 48

试试下面的代码。应该可以。

object[] args = new object[]   { "Project", "ProjectName", "@PrName", "ProjectStartDate", "@StartDate", "ProjectEndDate", "@EndDate", "ProjectId", "@Id" };

如果有效请标记为答案。