开发者

passing datetime as a variable to a stored procedure from an aspx page

开发者 https://www.devze.com 2023-03-13 12:10 出处:网络
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 t

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();
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号