【问题标题】:SQLServerCE cannot use rtrim in select statement - SQLCEExceptionSQLServerCE 不能在 select 语句中使用 rtrim - SQLCEException
【发布时间】:2012-02-03 15:58:15
【问题描述】:

我正在编写一个使用 SQL CE 的 Windows 移动应用程序。当我包含 WHERE Barcode = @Barcode 语句时,它没有返回任何行。

我猜这是因为 Barcode 的值后面有空格。所以我想使用WHERE rtrim(Barcode) LIKE @Barcode。但它给了我一个SqlCeException 说“函数的指定参数值无效。”

我确定我在这里遗漏了一些愚蠢的东西。非常感谢任何帮助。

这是我的代码:

using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlServerCe;

namespace ElectricBarcodeApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void buttonStart_Click(object sender, EventArgs e)
        {
            System.Data.SqlServerCe.SqlCeConnection conn = new System.Data.SqlServerCe.SqlCeConnection(
            ("Data Source=" + (System.IO.Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase), "ElectricReading.sdf") + ";Max Database Size=2047")));
            try
            {
                // Connect to the local database
                conn.Open();
                System.Data.SqlServerCe.SqlCeCommand cmd = conn.CreateCommand();

                SqlCeParameter param = new SqlCeParameter();
                param.ParameterName = "@Barcode";
                param.Value = textBarcode.Text.Trim();



                // Insert a row
                cmd.CommandText = "SELECT Location, Reading FROM Main2 WHERE rtrim(Barcode) LIKE @Barcode";
                cmd.Parameters.Add(param);

                cmd.ExecuteNonQuery();

                DataTable data = new DataTable();

                using (SqlCeDataReader reader = cmd.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        data.Load(reader);
                    }
                }
                if (data != null)
                {
                    this.dataGrid1.DataSource = data;
                }

            }

            finally
            {
                conn.Close();
            }



        }

        private void Form1_Load(object sender, EventArgs e)
        {
            if (ElectricReadingDataSetUtil.DesignerUtil.IsRunTime())
            {
                // TODO: Delete this line of code to remove the default AutoFill for 'electricReadingDataSet.Main2'.
                this.main2TableAdapter.Fill(this.electricReadingDataSet.Main2);
            }

        }
    }
}

【问题讨论】:

    标签: c# windows-mobile sql-server-ce sqlexception


    【解决方案1】:

    SqlCE 支持rtrim,所以这不应该是你的问题。你真的需要在这里做一个LIKE吗?如果将LIKE 替换为=,代码会运行吗?

    【讨论】:

    • 否,因为 sqlce 数据库已将其定义为 ntext,这不适用于 =,我想我会更改数据类型并尝试一下
    • 改成 = 还是没有变化
    • 嗯,您的 Barcode 列中有任何 NULL 值吗?可能 rtrim 不适用于 NULL。
    【解决方案2】:

    问题是您不能对 NTEXT 或 TEXT 列进行 RTRIM。这也适用于标准 SQL Server。

    您必须先将其转换为 NVARCHAR:

    SELECT Location, Reading FROM Main2 WHERE rtrim(CONVERT(NVARCHAR, Barcode)) LIKE @Barcode
    

    【讨论】:

      【解决方案3】:

      你不能替换这个吗:

      cmd.CommandText = "SELECT Location, Reading FROM Main2 WHERE rtrim(Barcode) LIKE @Barcode"; 
      

      有了这个(注意我删除了 rtrim):

      cmd.CommandText = "SELECT Location, Reading FROM Main2 WHERE Barcode LIKE @Barcode"; 
      

      然后是这个:

      param.Value = textBarcode.Text.Trim();
      

      用这个(添加通配符以便 LIKE 可以匹配它):

      param.Value = textBarcode.Text.Trim() + "%";
      

      【讨论】:

      • 我会根据 CONVERT 选项来测试它的性能。老实说,我不知道哪个更快(尽管我个人会选择根本不使用 SQL,而是使用 TableDirectSeek,这将比 either 方法更快数量级)。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-15
      • 1970-01-01
      • 2011-07-27
      • 1970-01-01
      相关资源
      最近更新 更多