我怎样才能毫无错误地使用oledb?

时间:2015-04-15 06:19:46

标签: c# asp.net

我有mdb和oracle数据库的oledbconnection,如下所示:

OleDbConnection Connection;
Connection = new OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0;Data Source=" +
                                 Server.MapPath("~/db.mdb"));

OleDbCommand Command1, Command2;

Command1 = new OleDbCommand("SELECT d1 FROM Table1 WHERE ID = 1", Connection);
Command2 = new OleDbCommand("SELECT d1 FROM Table1 WHERE ID = 2", Connection);

Connection.Open();

var1 = (int)Command1.ExecuteScalar();
var2 = (int)Command2.ExecuteScalar();

Connection.Close();

当可以连接到数据库并选择返回值时,它工作得很好。

但是当连接到数据库或表的问题不存在时,页面获取错误。

我在gridview和detailsview控件中使用var1和var2。

当连接问题或表不存在时,我需要打开此页面而不会出错。例如,如果页面无法连接到数据库或表不存在,则var1和var2值可以为0.并且页面打开时没有错误。

像这样:

Connected? --> yes --> return any value oledbcommand? --> yes --> var1 = thisvalue

Connected? --> no --> var1 = 0

Connected? --> yes --> return any value? --> no(table does not exist etc.) --> var1 = 0

我该怎么做?

2 个答案:

答案 0 :(得分:2)

也许您可以使用尝试抓取来确保您的网页无误地打开。

    bool con = true;
    try
    {
        OleDbConnection Connection;
        Connection = new OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0;Data Source=" +
                                         Server.MapPath("~/db.mdb"));
        Connection.Open();

        OleDbCommand Command1, Command2;
        Command1 = new OleDbCommand("SELECT d1 FROM Table1 WHERE ID = 1", Connection);
        Command2 = new OleDbCommand("SELECT d1 FROM Table1 WHERE ID = 2", Connection);
        try
        {
            var1 = (int)Command1.ExecuteScalar();
            var2 = (int)Command2.ExecuteScalar();
        }
        catch
        {
            con = false;
        }
        finally
        {
            Command1.Dispose();
            Command2.Dispose();
            Connection.Close();
        }
    }
    catch
    {
        con = false;
    }
    finally
    {
        if (!con)
        {
            //put your static value here
            var1 = 1;
            var2 = 2;
        }
    }

答案 1 :(得分:1)

我们可以通过using statement/try/catch阻止来执行此操作。

        try
        {
            OleDbConnection Connection;
            using (Connection = new OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0;Data Source=" +
                                             Server.MapPath("~/db.mdb")))
            {
                OleDbCommand Command1, Command2;

                Command1 = new OleDbCommand("SELECT d1 FROM Table1 WHERE ID = 1", Connection);
                Command2 = new OleDbCommand("SELECT d1 FROM Table1 WHERE ID = 2", Connection);

                Connection.Open();

                var1 = (int)Command1.ExecuteScalar();
                var2 = (int)Command2.ExecuteScalar();

                Connection.Close();
            }
        }
        catch (Exception)
        {
            //Log your exception here
            var1 = 1;
            var2 = 2;
        }

using语句内部处理Exception。根据{{​​3}},

  

using语句确保即使调用Dispose也会调用   在对象上调用方法时发生异常。您可以   通过将对象放在try块中来实现相同的结果   然后在finally块中调用Dispose;其实这是怎么回事   using语句由编译器翻译。