如何在虚拟桌子的两个日期选择器之间获取数据(A place Holder)

时间:2015-11-02 07:58:58

标签: c# sql sql-server

所以我在我的C#代码中有一个sql语句来尝试在两个日期范围之间提取数据。唯一的问题是,什么都没有出现。

        OpenConnection(conn);
        DataTable dt = new DataTable();
        SqlCommand cmd = new SqlCommand("Select CampaignName AS 'CAMPAIGN NAME', campaignDescription AS 'CAMPAIGN DESCRIPTION', CASE WHEN EndDate >= GETDATE() and StartDate <= GETDATE() THEN 'ACTIVE' WHEN StartDate >= GETDATE() THEN 'PENDING' ELSE 'CLOSED' END as 'CURRENT STATUS', CONVERT(VARCHAR(11), StartDate,106) + ' - ' + CONVERT(VARCHAR(11),EndDate,106) AS 'CAMPAIGN DATES', Discount AS 'DISCOUNT', [Target] AS 'TARGET', Uptake AS 'UPTAKE', AddedBy AS 'ADDED BY', DateAdded AS 'DATE ADDED' FROM Tbl_Campaign WHERE startDate BETWEEN @from AND @to", conn);

        try
        {

            SqlParameter param;
            param = new SqlParameter("@from", SqlDbType.DateTime);
            param.Value = txtStartDate.Text;
            cmd.Parameters.Add(param);
            param = new SqlParameter("@to", SqlDbType.DateTime);
            param.Value = txtDateEnd.Text;
            cmd.Parameters.Add(param);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(dt);
        }
        finally
        {
            cmd.Dispose();
            conn.Close();
            conn.Dispose();
        }   

如果您滚动到我的Sql命令的末尾,您将看到我和她之间的两个变量。我尝试在SQL中执行这段代码,如果我使用''引号手动解析日期,它就可以工作。我认为这就是它没有检索数据的原因。有人可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

我使用了一个文本框来获取日期,因为您的代码包含&#34; txtStartDate.Text&#34;。因此,我只使用简单的查询来尝试您的代码,以获取日期范围内的数据。它运作良好。所以,我认为您的查询中的另一个地方存在问题。 这是我试过的代码:

        con = new SqlConnection();
        string _connectionString = GetConnectionString();
        con.ConnectionString = _connectionString;
        con.Open();
        DataTable dt = new DataTable();
        SqlCommand cmd = new SqlCommand("SELECT * FROM FORDSUMD WHERE Dated BETWEEN @from AND @to",con);

        try
        {
            SqlParameter param;
            param = new SqlParameter("@from", SqlDbType.DateTime);
            param.Value = txtStartDate.Text;
            cmd.Parameters.Add(param);
            param = new SqlParameter("@to", SqlDbType.DateTime);
            param.Value = txtDateEnd.Text;
            cmd.Parameters.Add(param);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(dt);
        }
        finally
        {
            cmd.Dispose();
            con.Close();
            con.Dispose();
        }   

    public string GetConnectionString(string databaseName = "", string serverName = "")
    {
        SqlConnectionStringBuilder conString = new SqlConnectionStringBuilder();

        if (string.IsNullOrEmpty(serverName))
            serverName = ConfigurationManager.AppSettings["ServerName"];

        //Assing SQl server name
        conString.DataSource = serverName;
        conString.InitialCatalog = string.IsNullOrEmpty(databaseName) ? ConfigurationManager.AppSettings["DBName"] : databaseName;
        conString.IntegratedSecurity = false;
        conString.UserID = ConfigurationManager.AppSettings["UserId"];
        conString.Password = ConfigurationManager.AppSettings["Password"];

        return conString.ConnectionString;
    }

如果要使用DateTimePicker控件而不是文本框控件,只需用dtpStart.Value(DateTimePicker)替换txtStartDate.Text,用dtpEnd.Value替换txtDateEnd.Text。它会起作用。