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提供程序,String格式,Object [] args)
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
答案 0 :(得分:0)
尝试以下代码。它应该工作。
object[] args = new object[] { "Project", "ProjectName", "@PrName", "ProjectStartDate", "@StartDate", "ProjectEndDate", "@EndDate", "ProjectId", "@Id" };
如果有效,请标记为答案。