我有一个注册页面,当用户点击提交按钮时,注册表单中的数据将存储在数据库中。我还需要在用户创建帐户时插入日期。如何在我的插入查询中包含'datetime.now ...
以下是我的代码:
private void ExecuteInsert(string streetAdr, string city, string state, int countryID, string zipCode, string TelNum, string FaxNum)
{
SqlConnection connectionString = new SqlConnection(GetConnectString());
try
{
connectionString.Open();
// Insert Address
string addressQuery = "Insert into Address (StreetAdd, City, State, CountryID, ZipCode, TelNum, FaxNum)" +
"VALUES (@stAd, @CityAdr, @StateAdr, @IDcountry, @zCode ,@Tnumb ,@Fnumb)";
SqlCommand addressCmd = new SqlCommand(addressQuery, connectionString);
SqlParameter[] address = new SqlParameter[7];
address[0] = new SqlParameter("@stAd", SqlDbType.VarChar, 100);
address[1] = new SqlParameter("@CityAdr", SqlDbType.VarChar, 100);
address[2] = new SqlParameter("@StateAdr", SqlDbType.VarChar, 100);
address[3] = new SqlParameter("@IDcountry", SqlDbType.Int, 100);
address[4] = new SqlParameter("@zCode", SqlDbType.VarChar, 100);
address[5] = new SqlParameter("@Tnumb", SqlDbType.VarChar, 100);
address[6] = new SqlParameter("@Fnumb", SqlDbType.VarChar, 100);
address[0].Value = streetAdr;
address[1].Value = city;
address[2].Value = state;
address[3].Value = countryID;
address[4].Value = zipCode;
address[5].Value = TelNum;
address[6].Value = FaxNum;
for (int i = 0; i < address.Length; i++)
{
addressCmd.Parameters.Add(address[i]);
}
addressCmd.CommandType = CommandType.Text;
addressCmd.ExecuteNonQuery();
}
catch (Exception ex)
{
}
finally
{
connectionString.Close();
}
}
应该把datetime.now放在哪里?谢谢..
答案 0 :(得分:0)
您需要在数据库中构建一个日期列,并将日期作为参数传递给上面的其他字段,或者在数据库中为该字段指定默认值。
private void ExecuteInsert(string streetAdr, string city, string state, int countryID, string zipCode, string TelNum, string FaxNum)
{
SqlConnection connectionString = new SqlConnection(GetConnectString());
try
{
connectionString.Open();
// Insert Address
string addressQuery = "Insert into Address (StreetAdd, City, State, CountryID, ZipCode, TelNum, FaxNum, TransactioNDate)" +
"VALUES (@stAd, @CityAdr, @StateAdr, @IDcountry, @zCode ,@Tnumb ,@Fnumb, @Date)";
SqlCommand addressCmd = new SqlCommand(addressQuery, connectionString);
SqlParameter[] address = new SqlParameter[8];
address[0] = new SqlParameter("@stAd", SqlDbType.VarChar, 100);
address[1] = new SqlParameter("@CityAdr", SqlDbType.VarChar, 100);
address[2] = new SqlParameter("@StateAdr", SqlDbType.VarChar, 100);
address[3] = new SqlParameter("@IDcountry", SqlDbType.Int, 100);
address[4] = new SqlParameter("@zCode", SqlDbType.VarChar, 100);
address[5] = new SqlParameter("@Tnumb", SqlDbType.VarChar, 100);
address[6] = new SqlParameter("@Fnumb", SqlDbType.VarChar, 100);
address[7] = new SqlParameter("@Date", SqlDbType.DateTime);
address[0].Value = streetAdr;
address[1].Value = city;
address[2].Value = state;
address[3].Value = countryID;
address[4].Value = zipCode;
address[5].Value = TelNum;
address[6].Value = FaxNum;
address[7].Value = DateTime.Now();
for (int i = 0; i < address.Length; i++)
{
addressCmd.Parameters.Add(address[i]);
}
addressCmd.CommandType = CommandType.Text;
addressCmd.ExecuteNonQuery();
}
catch (Exception ex)
{
}
finally
{
connectionString.Close();
}
}