Hi guys. I am using the below query to run an sql statement.It works fine. I wish to know how I can modify the query to select by a date which the user enters in ASP.NET. This means that I need to modify the part:
嗨,家伙。我正在使用下面的查询来运行一条sql语句。它将正常工作。我想知道如何通过用户在ASP.NET中输入的日期来修改查询以进行选择。这意味着我需要修改部分:
WHERE TRANSACTION_DATE = '02-AUG-2006'
Any ideas please? Thanks for your help
有什么想法吗?谢谢你的帮助
QUERY I AM USING
查询我用
INSERT INTO TRANSACTION (TRX_UNIT, TRX_DATE, TRX_USR)
SELECT SOURCE_SYSTEM_CHANNEL_CODE, TRANSACTION_DATE, USER_CODE
FROM FCR_TRANSACTION
WHERE TRANSACTION_DATE = '02-AUG-2006'
3 个解决方案
#1
6
What about:
是什么:
// Create a connection object and data adapter
MySqlConnection cnx = new MySqlConnection(connectionString);
// Create a SQL command object
string cmdText = "INSERT INTO TRANSACTION (TRX_UNIT, TRX, TRX_USR) ";
cmdText += "SELECT SOURCE_SYSTEM_CHANNEL_CODE, TRANSACTION_DATE, USER_CODE ";
cmdText += "FROM FCR_TRANSACTION ";
cmdText += "WHERE TRANSACTION_DATE = @TransactionDate";
MySqlCommand cmd = new MySqlCommand(cmdText, cnx);
cmd .Parameters.Add("@TransactionDate", YourDate); // <-- Insert date here
// Set the command type to text
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
#2
0
you must have a name for the date field where the user enters a date. let us say you decide to name it 'datename' after that the $_POST("datename") returns the text entered in the field. so now the WHERE statement becomes
用户输入日期的日期字段必须有名称。假设您决定将它命名为“datename”,然后$_POST(“datename”)返回字段中输入的文本。现在WHERE语句变成了
WHERE TRANSACTION_DATE = '$_POST("datename")'
if that doesnt work, try
如果那不行,那就试试。
WHERE TRANSACTION_DATE = $_POST("datename")
all this is assuming you kept method of form transfer as POST....if you have it as GET, simply replace $_POST with $_GET here..
这一切都是假设你保存方法的形式转移帖子....如果你有一个GET,只需用$_GET替换$_POST。
This should work. If it doesnt, my apologies.
这应该工作。如果没有,我道歉。
#3
0
If you really want to query by the exact Date, you have to validate the Userinput, and add a parameter to the query (this is only pseudocode) :
如果您真的想按确切的日期进行查询,您必须验证Userinput,并向查询添加一个参数(这只是伪代码):
string userInput = txtDate.Text;
DateTime parsedDate;
if (DateTime.TryParse(userInput, out parsedDate)) {
// valid date, add a Parameter to your command
var cmd = ... // create your DB Command here
cmd.CommandText = "SELECT .... WHERE Transaction_Date = @txDate";
cmd .Parameters.Add("@txDate", parsedDate);
// execute your command ...
}
#1
6
What about:
是什么:
// Create a connection object and data adapter
MySqlConnection cnx = new MySqlConnection(connectionString);
// Create a SQL command object
string cmdText = "INSERT INTO TRANSACTION (TRX_UNIT, TRX, TRX_USR) ";
cmdText += "SELECT SOURCE_SYSTEM_CHANNEL_CODE, TRANSACTION_DATE, USER_CODE ";
cmdText += "FROM FCR_TRANSACTION ";
cmdText += "WHERE TRANSACTION_DATE = @TransactionDate";
MySqlCommand cmd = new MySqlCommand(cmdText, cnx);
cmd .Parameters.Add("@TransactionDate", YourDate); // <-- Insert date here
// Set the command type to text
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
#2
0
you must have a name for the date field where the user enters a date. let us say you decide to name it 'datename' after that the $_POST("datename") returns the text entered in the field. so now the WHERE statement becomes
用户输入日期的日期字段必须有名称。假设您决定将它命名为“datename”,然后$_POST(“datename”)返回字段中输入的文本。现在WHERE语句变成了
WHERE TRANSACTION_DATE = '$_POST("datename")'
if that doesnt work, try
如果那不行,那就试试。
WHERE TRANSACTION_DATE = $_POST("datename")
all this is assuming you kept method of form transfer as POST....if you have it as GET, simply replace $_POST with $_GET here..
这一切都是假设你保存方法的形式转移帖子....如果你有一个GET,只需用$_GET替换$_POST。
This should work. If it doesnt, my apologies.
这应该工作。如果没有,我道歉。
#3
0
If you really want to query by the exact Date, you have to validate the Userinput, and add a parameter to the query (this is only pseudocode) :
如果您真的想按确切的日期进行查询,您必须验证Userinput,并向查询添加一个参数(这只是伪代码):
string userInput = txtDate.Text;
DateTime parsedDate;
if (DateTime.TryParse(userInput, out parsedDate)) {
// valid date, add a Parameter to your command
var cmd = ... // create your DB Command here
cmd.CommandText = "SELECT .... WHERE Transaction_Date = @txDate";
cmd .Parameters.Add("@txDate", parsedDate);
// execute your command ...
}