小さな日時であるArrivalDate列に文字列「09/21/3013」を挿入しようとするまで、すべて正常に挿入されています。データベースに挿入できるようにこれを変換するにはどうすればよいですか? 前もって感謝します!
コードは次のとおりです。
private void ExecuteInsert(string BookingName, string BookedBy)
{
string connString = System.Configuration.ConfigurationManager.ConnectionStrings["CateringAuthorizationEntities"].ConnectionString;
SqlConnection conn = new SqlConnection(connString);
string sql = "INSERT INTO tbBooking (BookingName, BookedBy, ArrivalDate) VALUES "
+ " (@BookingName, @BookedBy, @ArrivalDate)";
try
{
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
SqlParameter[] param = new SqlParameter[3];
//param[0] = new SqlParameter("@id", SqlDbType.Int, 20);
param[0] = new SqlParameter("@BookingName", System.Data.SqlDbType.VarChar, 50);
param[1] = new SqlParameter("@BookedBy", System.Data.SqlDbType.VarChar, 50);
param[2] = new SqlParameter("@ArrivalDate", System.Data.SqlDbType.SmallDateTime);
param[0].Value = BookingName;
param[1].Value = BookedBy;
param[2].Value = ArrivalDate;
for (int i = 0; i < param.Length; i++)
{
cmd.Parameters.Add(param[i]);
}
cmd.CommandType = System.Data.CommandType.Text;
cmd.ExecuteNonQuery();
}
catch (System.Data.SqlClient.SqlException ex)
{
string msg = "Insert Error:";
msg += ex.Message;
throw new Exception(msg);
}
finally
{
conn.Close();
}
}
protected void BtnCatering_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
//call the method to execute insert to the database
ExecuteInsert(BookingName.Text, BookedBy.Text);
Response.Write("Record was successfully added!");
}
}