SQLServerCE 无法在 select 语句中使用 rtrim - SQLCEException

发布于 2024-12-25 15:44:15 字数 2563 浏览 1 评论 0原文

我正在编写一个使用 SQL CE 的 Windows Mobile 应用程序。当我包含 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);
            }

        }
    }
}

I am writing a windows mobile application that uses SQL CE. It is not returning any rows when I include the WHERE Barcode = @Barcode statement.

I am guessing this is because the value for Barcode has trailing spaces after it. So I want to use WHERE rtrim(Barcode) LIKE @Barcode. But it is giving me a SqlCeException saying "The specified argument value for the function is not valid."

I'm sure I am missing something stupid here. Any help greatly appreciated.

Here is my code:

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);
            }

        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

活泼老夫 2025-01-01 15:44:15

问题是您无法 RTRIM NTEXT 或 TEXT 列。这也适用于标准 SQL Server。

您必须先将其转换为 NVARCHAR:

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

The problem is that you can't RTRIM an NTEXT or TEXT column. This also applies to standard SQL Server.

You have to convert it to NVARCHAR first:

SELECT Location, Reading FROM Main2 WHERE rtrim(CONVERT(NVARCHAR, Barcode)) LIKE @Barcode
草莓味的萝莉 2025-01-01 15:44:15

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

SqlCE supports rtrim, so that shouldn't be your problem. Do you really need to do a LIKE here? Does the code run if you replace LIKE with =?

转身泪倾城 2025-01-01 15:44:15

你不能

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() + "%";

Can't you replace this:

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

with this (note I removed rtrim):

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

and then this:

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

with this (adding the wildcard so the LIKE can match it):

param.Value = textBarcode.Text.Trim() + "%";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文