Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Like a C# <code>DateTime</code> value, a SQL Server <code>DateTime</code> value has no format: it's just a 64-bit field consisting of two 32-bit integers. The first counts days since the epoch (1 January 1900 00:00:00.000); the second counts time since start of day in 1/300ths of a second.</p> <p>You apply formatting when you display it or convert it to char/varchar, either in SQL using <a href="http://msdn.microsoft.com/en-us/library/ms187928%28v=sql.100%29.aspx" rel="nofollow">CONVERT()</a>, or in the client code.</p> <p>Your <code>update</code> statement doesn't care about format: it cares about value. If you pass a C# <code>DateTime</code> value as a <code>SqlParameter</code> to your stored procedure or parameterized query, the Right Thing will happen: the CLR will magically translate the one into the other for you.</p> <p>If, you pass a string as the DateTime parameter from C#, it needs to be in a format that SQL Server will recognize as a DateTime string. Assuming that's the case, the conversion from C# string to SQL Server DateTime value will likewise happen magically.</p> <p>Given your <code>update</code> statement, code something like the following ought to do you:</p> <pre><code>public int UpdateDiscount( int discountPromotionalID , bool isActive , string title , DateTime beginDate , DateTime endDate , int discountPercentage ) { const string updateQuery = @" Update discount set DiscountPromotionalID = @DiscountPromotionalID , isActive = @isActive , Title = @Title , BeginDate = @BeginDate , EndDate = @EndDate , DiscountPercentage = @DiscountPercentage where DiscountPromotionalID = @DiscountPromotionalID " ; int rowsAffected ; using ( SqlConnection connection = new SqlConnection( SomeConnectString ) ) using ( SqlCommand cmd = connection.CreateCommand() ) { cmd.CommandText = updateQuery ; cmd.CommandType = CommandType.Text ; cmd.Parameters.AddWithValue( "@DiscountPromotionalID" , discountPromotionalID ) ; cmd.Parameters.AddWithValue( "@isActive" , isActive ? 1 : 0 ) ; // C# bools are true/false; SQL bools are 1/0 cmd.Parameters.AddWithValue( "@Title" , title ) ; cmd.Parameters.AddWithValue( "@BeginDate" , beginDate ) ; cmd.Parameters.AddWithValue( "@EndDate" , endDate ) ; cmd.Parameters.AddWithValue( "@DiscountPercentage" , discountPercentage ) ; connection.Open() ; rowsAffected = cmd.ExecuteNonQuery() ; connection.Close() ; } return rowsAffected ; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload