如何将数据从datagridview获取到sqlite数据库 [英] How do I get my data from datagridview to sqlite database

查看:715
本文介绍了如何将数据从datagridview获取到sqlite数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将数据从datagridview保存到我的SQLite数据库,但是有一些错误。



数据无法保存到数据库中并且出现错误消息盒子不断弹出



我尝试过:



 SQLiteConnection sa = new SQLiteConnection(Data Source = | DataDirectory | \\trendchart.db); 
sa.Open();

使用(SQLiteConnection sqlconn = new SQLiteConnection(sa))
{

foreach(dataGridViewRow dataGridView1.Rows中的行)
{
for(int z = 0; z< 31; z ++)

{
using(SQLiteCommand sqlcomm = sqlconn.CreateCommand())
{

sqlcomm.CommandText =插入数据(TOP(1),TOP(2))值(@TOP(1),@ TOP(2));
try
{
sqlcomm.Parameters.AddWithValue(TOP(1),dataGridView1.CurrentRow.Cells [TOP(1)]。Value.ToString());
sqlcomm.Parameters.AddWithValue(TOP(2),dataGridView1.CurrentRow.Cells [TOP(2)]。Value.ToString());
sqlcomm.ExecuteNonQuery();
MessageBox.Show(数据保存,请检查thks);

}
catch(exception ex)
{
MessageBox.Show(Error Please fix);
}
}
}

解决方案

我很确定这是你已经命名你的列和你的SQL参数的方式。请尝试这样做

 sqlcomm.CommandText =  插入数据([TOP(1)],[TOP(2)])值(@ TOP1,@ TOP2); 
尝试
{
sqlcomm.Parameters.AddWithValue( < span class =code-string> @ TOP1,dataGridView1.CurrentRow.Cells [ TOP( 1)] Value.ToString())。
sqlcomm.Parameters.AddWithValue( @ TOP2,dataGridView1.CurrentRow.Cells [< span class =code-string>
TOP(2)]。Value.ToString());
sqlcomm.ExecuteNonQuery();

注意事项:

- 列名周围的方括号 - 否则就是非法的。

- @ 添加参数时的符号

- 删除参数名称中的(非法)字符 - TOP(1)变为 @TOP1


这是一种方法:便携式数据库(II):将SQLite与实体框架结合使用 [< a href =http://geekswithblogs.net/danielggarcia/archive/2013/12/22/portable-databases-ii-using-sqlite-with-entity-framework.aspx\"target =_ blanktitle =新窗口> ^ ]

I want to save my data from datagridview to my SQLite database but there is some errors.

The data could not get saved into the database and the error message box keeps popping up

What I have tried:

SQLiteConnection sa = new SQLiteConnection("Data Source=|DataDirectory|\\trendchart.db");
           sa.Open();

           using (SQLiteConnection sqlconn = new SQLiteConnection(sa))
           {

               foreach (DataGridViewRow row in dataGridView1.Rows)
               {
                   for (int z = 0; z < 31; z++)

                   {
                       using (SQLiteCommand sqlcomm = sqlconn.CreateCommand())
                       {

                           sqlcomm.CommandText = "Insert into data (TOP(1),TOP(2) ) values(@TOP(1),@TOP(2))";
                           try
                           {
                               sqlcomm.Parameters.AddWithValue("TOP(1)", dataGridView1.CurrentRow.Cells["TOP(1)"].Value.ToString());
                               sqlcomm.Parameters.AddWithValue("TOP(2)", dataGridView1.CurrentRow.Cells["TOP(2)"].Value.ToString());
                               sqlcomm.ExecuteNonQuery();
                               MessageBox.Show("Data is save please check thks ");

                           }
                           catch (Exception ex)
                           {
                               MessageBox.Show("Error Please fix ");
                           }
                       }
                   }

解决方案

I'm pretty sure it's the way you've named your columns and your sql parameters. Try this instead

sqlcomm.CommandText = "Insert into data ([TOP(1)],[TOP(2)] ) values(@TOP1,@TOP2)";
 try
 {
     sqlcomm.Parameters.AddWithValue("@TOP1", dataGridView1.CurrentRow.Cells["TOP(1)"].Value.ToString());
     sqlcomm.Parameters.AddWithValue("@TOP2", dataGridView1.CurrentRow.Cells["TOP(2)"].Value.ToString());
     sqlcomm.ExecuteNonQuery();

Points to Note:
- the square brackets around the column names - they'd be illegal otherwise.
- The @ symbols when adding the parameters
- Removal of the (illegal) characters in the parameter names - TOP(1) becomes @TOP1


This is one way to do it: Portable databases (II): using SQLite with Entity Framework[^]


这篇关于如何将数据从datagridview获取到sqlite数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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