passing datetime as a variable to a stored procedure from an aspx page
I have the following code, and I am trying to pass datetime as a variable to a stored procedure. I have tried several different things with no luck. Any idea as to get date to pass to "@LVDate", and time to pass to "@LVTime"
string connectionString = "server=abc;database=abc;uid=abc;pwd=1234";
SqlConnection mySqlConnection = new Sq开发者_JS百科lConnection(connectionString);
string procedureString = "LV_Insert";
SqlCommand mySqlCommand = mySqlConnection.CreateCommand();
mySqlCommand.CommandText = procedureString;
mySqlCommand.CommandType = CommandType.StoredProcedure;
mySqlCommand.Parameters.Add("@LVDate", SqlDbType.DateTime).Value = DateTime.Now;
mySqlCommand.Parameters.Add("@LVTime", SqlDbType.DateTime).Value = DateTime.Now;
mySqlConnection.Open();
mySqlCommand.ExecuteNonQuery();
SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter();
mySqlDataAdapter.SelectCommand = mySqlCommand;
mySqlConnection.Close();
Also, I need to get them to pass in the following formats:
{0:MM/dd/yyyy},{0:HH:mm:ss}
What kind of error are you getting? It could be that DateTime.Now is not in the correct format that your stored procedure requires. Use String.Format;
String.Format("{0:MM/dd/yyyy", DateTime.Now);
String.Format("{0:HH:mm:ss", DateTime.Now);
string connectionString = "server=abc;database=abc;uid=abc;pwd=1234";
SqlConnection mySqlConnection = new SqlConnection(connectionString);
string procedureString = "LV_Insert";
SqlCommand mySqlCommand = mySqlConnection.CreateCommand();
mySqlCommand.CommandText = procedureString;
mySqlCommand.CommandType = CommandType.StoredProcedure;
mySqlCommand.Parameters.Add( new SqlParameter("@LVDate", DateTime.Now));
mySqlCommand.Parameters.Add(new SqlParameter("@LVTime", DateTime.Now));
mySqlConnection.Open();
mySqlCommand.ExecuteNonQuery();
SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter();
mySqlDataAdapter.SelectCommand = mySqlCommand;
mySqlConnection.Close();
精彩评论