object args = new object[] { "Project", "ProjectName", "@PrName", "ProjectStartDate", "@StartDate", "ProjectEndDate", "@EndDate", "ProjectId", "@Id" };
// ******************此行導致拋出異常*******************將DateTime參數類型傳遞給數據庫時使用String.Format。拋出異常。哪裏不對?
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
哪一行是你得到的消息? –
什麼是*確切的錯誤信息*和*它發生在哪裏*?你沒有使用String.Format,它是如何關聯的?註釋掉的代碼是否以某種方式相關? –