如何在C#中检查SQLite异常号? [英] How to check SQLite exception number in C#?

查看:61
本文介绍了如何在C#中检查SQLite异常号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我希望我写一个正确的论坛.

I hope I write into a correct forum.

所以>如果在sql命令中有问题(例如>某些字段不能为null或如果某些字段是uniqe等),如何检查这些异常号?像...

So> If is some problem in sql command (for example > some field can not be null OR if some field is uniqe etc.) how to check these exception numbers? Something like ...

try
{
}
catch(SQLiteException ex)
{
    switch(ex.Number)
    {
        case ***:
            do something
        case ***:
            do something
    }
}


谢谢


Thank you

推荐答案

您可以使用 SqlException.Number 属性以获取错误号.

You could use the SqlException.Number Property to get the error number.

            try
            {
                command.Connection.Open();
                command.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                for (int i = 0; i < ex.Errors.Count; i++)
                {
                    errorMessages.Append("Index #" + i + "\n" +
                        "Message: " + ex.Errors[i].Message + "\n" +
                        "Error Number: " + ex.Errors[i].Number + "\n" +
                        "LineNumber: " + ex.Errors[i].LineNumber + "\n" +
                        "Source: " + ex.Errors[i].Source + "\n" +
                        "Procedure: " + ex.Errors[i].Procedure + "\n");
                }
                Console.WriteLine(errorMessages.ToString());
            }


这篇关于如何在C#中检查SQLite异常号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆