当 sql 命令中的条件为阿拉伯语时,sqldatareader 出现其他问题

发布于 2024-09-25 22:19:46 字数 179 浏览 4 评论 0原文

当 sql 命令中的条件是阿拉伯语时,sqldatareader 会出现问题:

select user_name from users where typ=N 'arabic text'

尽管存在具有此类型的用户名,但它不会检索任何数据
那么你能帮我吗? 谢谢^_^

There is a problem with sqldatareader when the condtion in sql command is arabic like this:

select user_name from users where typ=N 'arabic text'

This does not retrieve any data although there is a user name which has this type
so can you help me please ?
thank ^_^

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

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

发布评论

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

评论(1

心是晴朗的。 2024-10-02 22:19:46

我已经重写了您的代码以使用查询参数。您希望避免像这样将字符串直接放入查询中(无论如何,类型很可能应该被标准化)。主函数可能应该将客户端列表返回到表示层,然后表示层将负责显示客户端,但我没有走那么远。

由于您的查询是在 Management Studio 中运行的,因此您的数据库排序规则设置应该没问题。

public void ListClientsByType(string clientType)
{
    using (IDbConnection conn = new SqlConnection("connectionstring"))
    {
        IDbCommand cmd = conn.CreateCommand();
        cmd.CommandText = "SELECT nam FROM clients WHERE typ = @type";

        cmd.Parameters.Add(
            _CreateInputParameter(cmd, DbType.String, "@type", clientType));

        conn.Open();

        IDataReader dr = null;

        try
        {
            dr = cmd.ExecuteReader();

            // Work with results here
        }
        finally
        {
            if (dr != null)
                dr.Close();
        }
    }
}

private IDbDataParameter _CreateInputParameter(
    IDbCommand cmd, DbType type, string name, object value)
{
    IDbDataParameter p = cmd.CreateParameter();

    p.DbType = DbType.String;
    p.Direction = ParameterDirection.Input;
    p.ParameterName = name;
    p.Value = value;

    return p;
}

I've rewritten your code to use a query parameter. You want to avoid putting strings directly into your queries like that (the type should most likely be normalized anyway). The main function should probably return a list of clients to your presentation layer, which would then be responsible for displaying the clients, but I didn't go that far.

Since your query worked from within Management Studio, your database collation settings should be okay.

public void ListClientsByType(string clientType)
{
    using (IDbConnection conn = new SqlConnection("connectionstring"))
    {
        IDbCommand cmd = conn.CreateCommand();
        cmd.CommandText = "SELECT nam FROM clients WHERE typ = @type";

        cmd.Parameters.Add(
            _CreateInputParameter(cmd, DbType.String, "@type", clientType));

        conn.Open();

        IDataReader dr = null;

        try
        {
            dr = cmd.ExecuteReader();

            // Work with results here
        }
        finally
        {
            if (dr != null)
                dr.Close();
        }
    }
}

private IDbDataParameter _CreateInputParameter(
    IDbCommand cmd, DbType type, string name, object value)
{
    IDbDataParameter p = cmd.CreateParameter();

    p.DbType = DbType.String;
    p.Direction = ParameterDirection.Input;
    p.ParameterName = name;
    p.Value = value;

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