今日の日付を挿入しようとしていますが、再帰的に日付を増やしてみています。変換エラーメッセージが表示されます。データベースにGetDate()を挿入しようとするとエラーが発生します
private void InsertTimesheetWeek(string timeSheetID)
{
int row = GViewTimeSheet.Rows.Count;//get the row count
int counter = 0;
string[] txtDate = new string[row];// date column
foreach (GridViewRow gRow in GViewTimeSheet.Rows)
{
txtDate[counter] = "GetDATE()+"+counter;
counter++;
}
//Intializing sql statement
string fields = "(TimeSheetID, Date)";
string parm = "(@TimeSheetID, @Date)";
string sqlStatement = "insert into TimeSheetWeeks" + fields + "Values" + parm;
SqlCommand comm = new SqlCommand();
comm.CommandText = sqlStatement;//assing sql statement as command
SqlConnection connection = DataAccess.getConnection();
comm.Connection = connection;
try
{
connection.Open();
for (int i = 0; i < row; i++)
{
comm.Parameters.AddWithValue("@TimeSheetID", timeSheetID);
comm.Parameters.AddWithValue("@Date", txtDate[i]);
comm.ExecuteNonQuery();
comm.Parameters.Clear();
}
}
catch (Exception ex)
{
Utilities.LogError(ex);
throw ex;
}
finally
{
if (connection.State == ConnectionState.Open)// if the connection opened then
{
connection.Close();//just close the connection in any way
}
}
}
なぜエラーが発生しますか?
を得ています?また、ブレークポイントで実行して、** "@ Date"、txtDate [i] **、おそらく** txtDate [i] **内の値をチェックしてください。エラーが発生します –
エラーメッセージ:文字列から日付や時刻を変換しています。 "私はデバッグを試して、すべてのものが良い(値 "GetDATE()+ 0"、 "GetDATE()+ 1"、 "GetDATE()+ 2" ....) – Sas