C#从新表单保存到SQLite

时间:2017-06-07 18:04:23

标签: c# visual-studio sqlite listview

我在SQLite中有一个小项目。我在ListView中显示了所有这些示例:

SQLiteConnection oSQLiteConnection =
                new SQLiteConnection("Data Source=database.s3db");
            SQLiteCommand oCommand = oSQLiteConnection.CreateCommand();
            oCommand.CommandText = "SELECT * FROM PERSONS";
            m_oDataAdapter = new SQLiteDataAdapter(oCommand.CommandText,
                oSQLiteConnection);
            SQLiteCommandBuilder oCommandBuilder =
                new SQLiteCommandBuilder(m_oDataAdapter);
            m_oDataSet = new DataSet();
            m_oDataAdapter.Fill(m_oDataSet);
            m_oDataTable = m_oDataSet.Tables[0];
            lstItems.DataContext = m_oDataTable.DefaultView;

我还有一个按钮,打开新表单,其中包含名称,用户名等字段,我想点击保存(在新的打开表单中)并将新行保存到我的数据库并更新此视图。我怎样才能做到这一点?在我的" min" viewI可以像这样做ststaic

DataRow oDataRow = m_oDataTable.NewRow();
oDataRow[1] = "Name";
oDataRow[2] = "Username";
m_oDataTable.Rows.Add(oDataRow);
m_oDataAdapter.Update(m_oDataSet);

如何从新表单中保存?我不能这样做:(

1 个答案:

答案 0 :(得分:0)

看看雇用 http://www.c-sharpcorner.com/UploadFile/718fc8/save-delete-search-and-update-record-in-ado-net/

我喜欢使用实体框架

 using (var context = new Db())
        {
            try
            {
               ...
                context.SaveChanges();
            }
            catch (Exception e)
            {

            }
        }