SQLiteExceptiopn未被用户代码处理

时间:2017-05-07 16:40:13

标签: c# sqlite data-binding datatable exception-handling

我尝试将新记录从网格更新/插入SQLite数据库。

以下是我绑定网格的方法:

public System.Windows.Forms.BindingSource bS = new System.Windows.Forms.BindingSource();
public SQLiteDataAdapter dataAdapter = new SQLiteDataAdapter();

        public BindingSource GetData(string query)
        {
            try
            {
                dataAdapter = new SQLiteDataAdapter(query, this.connectionString);
                SQLiteCommandBuilder commandBuilder = new SQLiteCommandBuilder(dataAdapter);

                DataTable table = new DataTable();
                dataAdapter.Fill(table);
                bS.DataSource = table;
            } catch (SQLiteException ex)
            {
                MessageBox.Show(ex.ToString());
            }

            return bS;
        }

这就是我将绑定源附加到网格的方式:

gridSirovina.DataSource = GetData.GetData(query);

当我尝试保存新记录时,我就是这样做的:

try
{
    bnd.dataAdapter.Update((DataTable)bnd.bS.DataSource);
}
catch (SQLiteException ex)
{
    MessageBox.Show(ex.ToString());
}

如果我尝试插入数据库的记录之一与已输入的ID相同,则应用程序会中断并出现错误:

  
    

类型' System.Data.SQLite.SQLiteException'的例外情况发生在System.Data.SQLite.dll中但未在用户代码中处理

  
     

其他信息:约束失败

     

UNIQUE约束失败:myTable.ID

更新
这是一个例外细节:

  

System.Data.SQLite.SQLiteException未被用户代码
处理   HResult = -2147467259消息=约束失败UNIQUE约束   失败:tblRoba.SifraRobe Source = System.Data.SQLite ErrorCode = 19
  堆栈跟踪:          在System.Data.SQLite.SQLite3.Reset(SQLiteStatement stmt)          在System.Data.SQLite.SQLite3.Step(SQLiteStatement stmt)          在System.Data.SQLite.SQLiteDataReader.NextResult()          在System.Data.SQLite.SQLiteDataReader..ctor(SQLiteCommand cmd,CommandBehavior表现)          在System.Data.SQLite.SQLiteCommand.ExecuteReader(CommandBehavior   行为)          在System.Data.SQLite.SQLiteCommand.ExecuteNonQuery(CommandBehavior   行为)          在System.Data.SQLite.SQLiteCommand.ExecuteNonQuery()          在System.Data.Common.DbDataAdapter.UpdateRowExecute(RowUpdatedEventArgs)   rowUpdatedEvent,IDbCommand dataCommand,StatementType cmdIndex)          在System.Data.Common.DbDataAdapter.Update(DataRow [] dataRows,DataTableMapping tableMapping)InnerException:

我想知道为什么我不能在try ... catch block中拦截这个错误?

0 个答案:

没有答案