我需要使用SQL查询DataGridView
,但不要向DataGridView
显示。
public chkTime()
{
InitializeComponent();
}
HRTaffDataContext db = new HRTaffDataContext();
SqlConnection Conn;
SqlCommand cmd = new SqlCommand();
SqlDataAdapter da;
DataTable dt = new DataTable();
DataSet ds = new DataSet();
StringBuilder sb = new StringBuilder();
string appConn = ConfigurationManager.ConnectionStrings["connDB"].ConnectionString;
string strDate;
private void chkTime_Load(object sender, EventArgs e)
{
connStr();
return;
}
public void connStr()
{
Conn = new SqlConnection();
if (Conn.State == ConnectionState.Open)
{
Conn.Close();
}
Conn.ConnectionString = appConn;
Conn.Open();
}
private void button2_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(appConn);
string sql = "SELECT [filesTA].EmpNo,[Employee].[First Name],[filesTA].ChkDate,[filesTA].ChkIn,[filesTA].ChkOut,[CompanyData].ShortName"
+ " From [WebSP].[dbo].[filesTA] inner join [WebSP].[dbo].[Employee] on [Employee].EmployeeNo=[filesTA].EmpNo INNER JOIN [WebSP].[dbo].[CompanyData] On [CompanyData].Company = [Employee].Company"
+ " WHERE [filesTA].ErrorCode = 0"; // It's work
+ " WHERE [filesTA].ErrorCode = 0 and [filesTA].ChkDate ='" + dateTimePicker.Text.ToString() + "'";
da = new SqlDataAdapter(sql, Conn);
DataSet ds = new DataSet();
da.Fill(ds);
Conn.Close();
dgvShow.DataSource = ds.Tables[0];
}
"WHERE [filesTA].ErrorCode = 0"
运行正常。
"WHERE [filesTA].ErrorCode = 0 and [filesTA].ChkDate ='" + dateTimePicker.Text.ToString() + "'"
不起作用。
我需要设置DateTime
。
答案 0 :(得分:1)
$ dateTimePicker.Text返回一个供人阅读的字符串,您的服务器可能不喜欢它。
尝试类似:
string sql = string.Format("SELECT [filesTA].EmpNo,[Employee].[First Name],[filesTA].ChkDate,[filesTA].ChkIn,[filesTA].ChkOut,[CompanyData].ShortName"
+ " From [WebSP].[dbo].[filesTA] inner join [WebSP].[dbo].[Employee] on [Employee].EmployeeNo=[filesTA].EmpNo INNER JOIN [WebSP].[dbo].[CompanyData] On [CompanyData].Company = [Employee].Company"
+ " WHERE [filesTA].ErrorCode = 0 and [filesTA].ChkDate ='{0}-{1}-{2}'",
dateTimePicker.Value.Year,
dateTimePicker.Value.Month,
dateTimePicker.Value.Day);
答案 1 :(得分:0)
您需要使用dateTimePicker.Value.ToString()
代替dateTimePicker.Text.ToString()
使用此功能的一个简单示例可以是dateTimePicker.Value.ToString("yyyy-MM-dd")