我的SqlServer数据库中有一个以下表模式。
COLUMN_NAME DATA_TYPE
Application_Date date
MY_TABLE
ID Application_Date Employee_Name
1 2014-05-16 a
2 2014-05-26 b
我正在尝试使用Button_Click()事件在My_Table中的那些日期数据类型之间搜索一些数据,并将数据绑定到我的JQGrid中。 我已经分别使用AjaxCalendarExtender创建了两个文本框之类的动态Web控件。
如何将文本框日期输入转换为SqlServer Date输入,以便为Search Button_Click()事件中的SQL注入提供输入。
我的C#代码:
protected void Button_Click(object sender, EventArgs e)
{
Panel2.Visible = true;
JQGrid10.Visible = true;
try
{
Table maintable = form1.FindControl("dynamic_filter_table_id") as Table;
int rc = maintable.Rows.Count;
if (rc == 1)
{
try
{
if (D1.SelectedValue.Contains("datetime"))
{
TextBox T1 = maintable.FindControl("DateFrom") as TextBox;
TextBox T2 = maintable.FindControl("DateTo") as TextBox;
DateTime inputone;
DateTime inputtwo;
inputone = DateTime.Parse(T1.Text);
inputtwo = DateTime.Parse(T2.Text);
**//How to parse for SqlServer Date datatype format **
SqlDataAdapter sql = new SqlDataAdapter("SELECT A.Col1,B.Employee_Name,B.Application_Date,B.ID, FROM RESULT as B, EMPLOYEE as A WHERE B.Application_Date Between " + inputone + " AND " + inputtwo, connectionstring);
DataSet data = new DataSet();
sql.Fill(data);
con.Close();
Session["DataforSearch_DDL"] = data.Tables[0];
}
}
catch
{
NotImplementedException();
}
}
}
}
答案 0 :(得分:2)
您可以尝试使用SqlCommand.Parameters。
var cmd = new SqlCommand(connectionstring);
cmd.CommandText = "SELECT A.Col1,B.Application_Date,B.ID, FROM RESULT as B, EMPLOYEE as A WHERE B.Application_Date, B.Employee_Name Between @inputone AND @inputtwo";
cmd.Parameters.Add("@inputone", SqlDbType.DateTime).Value = inputone;
cmd.Parameters.Add("@inputtwo", SqlDbType.DateTime).Value = inputtwo;