无法使用SQL Server 2008中的where子句获取nvarchar类型数据

时间:2014-04-19 08:26:47

标签: c# sql-server sql-server-2008 where between

string constr = Properties.Settings.Default.Subject_1ConnectionString;

SqlConnection conn = new SqlConnection(constr);
SqlCommand com = new SqlCommand("SELECT * from Subject_Title WHERE Date BETWEEN @hello and @hello1 ", conn);

// com.Parameters.Add("@hello", SqlDbType.NVarChar).Value = textBox1.Text;
// com.Parameters.Add("@hello1", SqlDbType.NVarChar).Value = textBox2.Text;

com.Parameters.Add("@hello", SqlDbType.NVarChar);
com.Parameters["@hello"].Value = textBox1.Text;

com.Parameters.Add("@hello1", SqlDbType.NVarChar);
com.Parameters["@hello1"].Value = textBox2.Text;

// com.Parameters.AddWithValue("@hello", textBox1.Text);
// com.Parameters.AddWithValue("@hello1", textBox2.Text);
SqlDataAdapter da = new SqlDataAdapter(com);
DataSet ds = new DataSet();
da.Fill(ds, "Subject_title");

for (int i = 0; i < 8; i++)
{
    this.labeltext = this.labeltext + " " + ds.Tables["Subject_Title"].Rows[i]["Date"].ToString();
    this.labeltext = this.labeltext + " " + ds.Tables["Subject_Title"].Rows[i]["Subject"].ToString();
    this.labeltext = this.labeltext + " ";
}

this.label1.Text = this.labeltext;

这里我没有从数据库中获取任何数据

Date是我的列名称,nvarchar类型,Subjecttext类型的另一列。

请任何人解决我的问题

2 个答案:

答案 0 :(得分:0)

我想你应该使用: Con.Open(); Con.Close();

但如果我是你,我会写这样的代码:

    string constr = Properties.Settings.Default.Subject_1ConnectionString;

    SqlConnection conn = new SqlConnection(constr);
    SqlCommand com = new SqlCommand("SELECT * from Subject_Title WHERE Date BETWEEN \"01-03-14\" and \"01-04-14\" ", conn);

    conn.Open();       
    SqlDataReader reader =com.ExecuteReader();
    while(reader.read()){

    this.labeltext += " " + reader.GetString(0);       //Use column ordinal for Date
    this.labeltext += " " + reader.GetString(1)+" ";   //Use column ordinal for Subject
    }
    conn.Close()
    this.label1.Text = this.labeltext;

答案 1 :(得分:0)

我试图为您提供更好的代码库。

你需要:

  • 使用更有意义的名称!像hellohello1这样的参数对于阅读代码的人来说并不是非常有用....还有:不要使用Date之类的保留关键字命名列 - 再次:使用更多内容对你的背景有意义

  • 如果要使用与日期相关的方法,则必须使用DATEDATETIME2(N)数据类型。如果您已将数据存储为nvarchar - 则必须先将其转换为DATE

  • 请始终将SqlConnectionSqlCommand放入using(...) { .. }块,以确保正确快速处置

  • 如果您只需要一个DataTable - 只需实例化DataTable并填写它 - 不要使用DataSet的不必要的额外开销 - 这只是浪费的资源...

代码:

string constr = Properties.Settings.Default.Subject_1ConnectionString;

// if you only need one single data table - use a DataTable - not a DataSet !
DataTable dt = new DataTable();

// *ALWAYS* put your SqlConnection and SqlCommand into using() blocks!
// also - if you want to use BETWEEN, you *MUST* use DATE!
// also: don't call your column "date" - that's a SQL Server reserved keyword! Use a more meaningful name 
// like "DateCreated" or "DateLastUpdated" or something
// and please also use more meaningful parameter names - "hello" and "hello1" is very confusing and not clear!!
using (SqlConnection conn = new SqlConnection(constr))
using (SqlCommand com = new SqlCommand("SELECT * FROM dbo.Subject_Title WHERE CAST(DateCreated AS DATE) BETWEEN @start and @end ", conn))
{
   // add parameters as DATE type!
   com.Parameters.Add("@start", SqlDbType.Date);
   com.Parameters["@start"].Value = DateTime.Parse(textBox1.Text).Date;

   com.Parameters.Add("@end", SqlDbType.Date);
   com.Parameters["@end"].Value = DateTime.Parse(textBox2.Text).Date;

   SqlDataAdapter da = new SqlDataAdapter(com);

   da.Fill(dt);
}

for (int i = 0; i < 8; i++)
{
    this.labeltext = this.labeltext + " " + dt.Rows[i]["Date"].ToString();
    this.labeltext = this.labeltext + " " + ds.Rows[i]["Subject"].ToString();
    this.labeltext = this.labeltext + " ";
}

this.label1.Text = this.labeltext;