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"
我有以下代码,我试图将datetime作为变量传递给存储过程。我尝试过几个不同的东西但没有运气。任何关于将日期传递给“@LVDate”的想法,以及传递给“@LVTime”的时间
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("@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}
2 个解决方案
#1
1
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;
你得到什么样的错误?可能是DateTime.Now的格式不正确,存储过程需要。使用String.Format;
String.Format("{0:MM/dd/yyyy", DateTime.Now);
String.Format("{0:HH:mm:ss", DateTime.Now);
#2
1
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();
#1
1
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;
你得到什么样的错误?可能是DateTime.Now的格式不正确,存储过程需要。使用String.Format;
String.Format("{0:MM/dd/yyyy", DateTime.Now);
String.Format("{0:HH:mm:ss", DateTime.Now);
#2
1
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();