当我想在C#windowForm中插入SQLite时,为什么会出现此错误? [英] Why this Error occur when I want INSERT into SQLite in C# windowForm ?

查看:92
本文介绍了当我想在C#windowForm中插入SQLite时,为什么会出现此错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用C#WindowsForm和Sqlite创建了一个应用程序。我可以删除,但是我不能插入记录。



我使用一个connectionString,并检查我的数据和sql查询是否插入。



所有这一切似乎都没问题,但我收到如下错误:



未处理的类型'异常' System.Data.SQLite.dll中发生System.InvalidOperationException



附加信息:由于对象的当前状态,操作无效。





这是我的InsertButton点击代码:



I created an app with C# WindowsForm and Sqlite. I can delete, but I can't insert records.

I use one connectionString, and I check my data and sql query for insert.

All of this seems OK, but I get an error such as this:

An unhandled exception of type 'System.InvalidOperationException' occurred in System.Data.SQLite.dll

Additional information: Operation is not valid due to the current state of the object.


and this is my InsertButton Click code :

if (catID == 0 || txtRating.Text ==null || txtback.Text==null||txtgenerel.Text==null||txtimportant.Text==null|| FileName==null)
            {
                MessageBox.Show("لطفا اطلاعات را کامل وارد کنید");
            }
            else{
                CopyImage(FileName);
                string q2 = "INSERT INTO Persons (CatID,FullName,Rating,BackGround,GeneralDetale,ImportantDetale,PicName) VALUES (" + catID + ","+txtFullName.Text+"," + txtRating.Text + "," + txtback.Text + "," + txtgenerel.Text + "," + txtimportant.Text + "," + imgnumber + ")";
            SQLiteCommand cmd1 = new SQLiteCommand(q2, cn);
            //try0
            //{
                cn.Open();
                cmd1.ExecuteNonQuery();
                cn.Close();
                Refresh();
            //}
            //catch
            //{
            //    MessageBox.Show("An Error Was Occured", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            //}
            }





错误在这一行:cn.open();

推荐答案

大多数情况下,你省略了撇号来包围字符串值。



最好的解决办法是使用参数化语句。



Mostly, you left out the apostrophes to surround the string values.

The best solution to this is to use a parameterized statement.

    CopyImage(FileName);
    string q2 = "INSERT INTO Persons (CatID,FullName,Rating,BackGround,GeneralDetale,ImportantDetale,PicName) VALUES ( @catID , @FullName , @Rating , @back , @txtgenerel , @important , @imgnumber)";
SQLiteCommand cmd1 = new SQLiteCommand(q2, cn);

cmd1.Parameters.AddWithValue ( "@CatID" , CatID ) ;
// Repeat for the other parameters


    cn.Open();
    cmd1.ExecuteNonQuery();
    cn.Close();


答案1解释您在修复连接问题时将面临的下一个问题。您可能共享相同的连接,当您尝试打开连接时,它可能已经打开或其他一些无法执行打开操作的状态。所有这些问题都是因为共享连接你最好遵循以下的做法。

Answer 1 explain about your next issue you will face when you fix connection issue. You may have sharing the same connection and when you try to open the connection it may be already open or some other state which can't do the open operation. All these issue because of the shared connection you better follow the below practice.
string sql = "INSERT INTO Persons (CatID,FullName,Rating,BackGround,GeneralDetale,ImportantDetale,PicName) VALUES ( @catID , @FullName , @Rating , @back , @txtgenerel , @important , @imgnumber)";
//don't reuse same connection over different methods, create new when you need
//and dispose it after use, using block will do it for you
using ( var oConn = new SqliteConnection (connectionString ) )
using( var oCmd = new SqliteCommand ( sql, oConn ))
{
    oConn.Open ();
    //set all parameter values

    //excute commad
    oCmd.ExecuteNonQuery ();
}


这篇关于当我想在C#windowForm中插入SQLite时,为什么会出现此错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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