SQLServerCE 无法在 select 语句中使用 rtrim - SQLCEException
我正在编写一个使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题是您无法 RTRIM NTEXT 或 TEXT 列。这也适用于标准 SQL Server。
您必须先将其转换为 NVARCHAR:
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:
SqlCE 支持
rtrim
,因此这不应该是您的问题。你真的需要在这里做一个LIKE
吗?如果将LIKE
替换为=
,代码是否会运行?SqlCE supports
rtrim
, so that shouldn't be your problem. Do you really need to do aLIKE
here? Does the code run if you replaceLIKE
with=
?你不能
用这个替换这个:(注意我删除了rtrim):
然后
用这个:替换这个(添加通配符以便LIKE可以匹配它):
Can't you replace this:
with this (note I removed rtrim):
and then this:
with this (adding the wildcard so the LIKE can match it):