当我想在C#windowForm中插入SQLite时

当我想在C#windowForm中插入SQLite时

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

问题描述

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



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



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





这是我的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:


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);
            //}
            }





推荐答案

    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();


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时,为什么会出现此错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 19:55