我正在尝试在c#中更新sqlite db文件

时间:2016-06-16 01:15:26

标签: c# sqlite datagridview

现在我正试图解决这个问题。但我不知道为什么它不起作用。

我已完成更改datagridview表单中的数据,但桌面上的sqlite文件未更改。

namespace Ex1
{
public partial class Form1 : Form
{
    int initValue = 0;
    String chkString;
    SQLiteConnection conn = new SQLiteConnection("Data Source=I:\\Coding\\VS\\Study1\\Ex1\\Ex1\\Test.db;Version=3;");
    SQLiteDataAdapter adapter = null;
    BindingSource bsOrigin = null;
    DataSet ds = null;
    Random rand = new Random();
    KnownColor[] Colors = (KnownColor[])Enum.GetValues(typeof(KnownColor));
    KnownColor randColor;

    public Form1()
    {
        InitializeComponent();

        textBox1.Text = initValue.ToString();            
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        bsOrigin = new BindingSource();
        ds = GetData();
        bsOrigin.DataSource = ds.Tables[0];
        dataGridView1.DataSource = bsOrigin;
    }

    private DataSet GetData()
    {
        adapter = new SQLiteDataAdapter("SELECT * from Skill", conn);
        DataSet ds = new DataSet();
        adapter.Fill(ds);
        return ds;            
    }

    private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
    {
        adapter = new SQLiteDataAdapter("SELECT * from Skill", conn);
        BindingSource bs = (BindingSource)dataGridView1.DataSource;
        DataTable dt = bs.DataSource as DataTable;

        adapter.Update(dt);


    }

}

}

有没有人能教我?

1 个答案:

答案 0 :(得分:0)

我使用这种方法:

public void saveData ()
{
SQLiteDataConnection con = new SQLiteDataConnection("yourconnectionstring");
SQLiteDataAdapter da= new SQLiteDataAdapter("Select statement", con);
SQLiteCommandBuiler com=new SQLiteCommandBuiler(da);
DataSet ds= new DataSet ();
DataTable dt = new DataTable ():
DataRow dr;

con.Open();
da.fill(ds, "tablename");
cn.Close();

dt=ds.Tables["tablename"];
dr=dt.NewRow();


dr["rowname"]=value;
//"same for the other rows"
dr.Rows.Add(dr);
da.Update(dt.Select(null, null, DataViewRowState.Added));

}

你还应该检查文件权限,我从sqlite看到了从某些文件夹访问的问题,桌面是一个。