//我开始用一个函数得到了两个时间,一个是开始时间(begintime),另一个是结束时间(entime)
public DateTime[] out_time()
    {
        DateTime[] between=new DateTime[2];
        string begintime = string.Format("{0}-{1}-{2} {3}:{4}:00", bgyear.Text, bgmonth.Text, bgdate.Text, bghour.Text, bgsecond.Text);
        DateTime begtime = Convert.ToDateTime(begintime);
        between[0]=begtime;
        string endtime = string.Format("{0}-{1}-{2} {3}:{4}:00", edyear.Text, edmonth.Text, eddate.Text, edhour.Text, edsecond.Text);
        DateTime entime = Convert.ToDateTime(endtime);
        between[1]= entime;
       return between;
    }
//后来我用她们的两个时间在数据库中查询数据
DateTime[] result = this.out_time();
                      SqlConnection conn = new SqlConnection();
        conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["eimserverdbConnectionString"].ConnectionString;
        SqlCommand scd = new SqlCommand();
        scd.Connection = conn;        scd.CommandText = "select * from cent_list where owner_regtime < result[1] and owner_regtime > result[0]";//标记处
        SqlDataAdapter sda = new SqlDataAdapter();
        sda.SelectCommand = scd;
conn.Open();
//atthis是一个gridview控件
            DataSet ds = new DataSet();
            sda.Fill(ds, "all");
            atthis.DataSource = ds.Tables["all"].DefaultView;
            atthis.DataBind();
conn.close();
但是结果什么也没有,好像报错在(“标记处”)请问大家,这个select语句该怎么写才是正确的?

解决方案 »

  1.   

    string begintime = string.Format("{0}-{1}-{2} {3}:{4}:00", bgyear.Text, bgmonth.Text, bgdate.Text, bghour.Text, bgsecond.Text);
    这个可以执行吗?记忆中string.Format只能带三个参数,超过三个就需要使用数组了
      

  2.   

    scd.CommandText = "select * from cent_list where owner_regtime < '" + result[1] + "' and owner_regtime > '" + result[0] + "'";
      

  3.   

    楼上真历害   SQL注入 一定很强   但这也是基本语法
      

  4.   

    别再SQL注入了,没意思啊,还是用存储过程吧。