Sqlite不支持的文件格式 [英] Sqlite unsuported file format

查看:257
本文介绍了Sqlite不支持的文件格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我在C#中有一个winforms应用程序我完成了,但现在我想减少sqlserver express等依赖项。

我要做的是将我的代码从sql server改为SQLite。

所以我谷歌了,并按照教程,我做了改动,但我有一些麻烦,任何人都可以看看代码,看看问题是什么?

我收到错误

Hi
I have a winforms app in C# witch i completed, but now i want to reduce the dependecies such as sqlserver express.
What i´m trying to do is alter my code from sql server to SQLite.
So i google´d it and followed a tutorial, i made the alterations but i´m having some trouble, can anyone take a look at the code and see what the problem is?
I get the error

An unhandled exception of type 'Finisar.SQLite.SQLiteException' occurred in SQLite.NET.dll
Additional information: unsupported file format




In the line sal_con.Open();



此表单用于获取来自的信息数据库名为db.db(我已经从sql转换为sqlit格式)表temp并将其打印到水晶报告报告。



如果我能得到这个工作形式其他表格将很容易迁移。

提前谢谢



我尝试过:



旧代码:


This form is suposed to get the info from a database called db.db (witch i already converted from sql to sqlit format) table "temp" and print it to a crystal reports report.

If i can get this form to work the other forms will be easy to migrate.
Thanks in advance

What I have tried:

OLD CODE:

    public partial class report : Form
    {
        public static SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\BD.mdf;Integrated Security=True;Connect Timeout=30");
        
public report()
        {
            InitializeComponent();
        }
        public void report_Load(object sender, EventArgs e)
        {
            con.Open();
            SqlCommand cmd = con.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "select * from temp";
            cmd.ExecuteNonQuery();
            bdDataSet ds = new bdDataSet();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(ds.temp);
            hytherm myReport = new hytherm();
            myReport.SetDataSource(ds);
            crystalReportViewer1.ReportSource = myReport;
            con.Close();

            con.Open();
            SqlCommand cmd2 = con.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "truncate table dbo.temp";
            cmd.ExecuteNonQuery();
            con.Close();
        }







新代码:




NEW CODE:

        private void SetConnection()
        {
            sql_con = new SQLiteConnection
                ("Data Source=db.db;Version=3");
        }

        private void ExecuteQuery(string txtQuery)
        {
            SetConnection();
            sql_con.Open();
            sql_cmd = sql_con.CreateCommand();
            sql_cmd.CommandText = txtQuery;
            sql_cmd.ExecuteNonQuery();
            sql_con.Close();
        }
        public void report_Load(object sender, EventArgs e)
        {
            SetConnection();
            sql_con.Open();
            sql_cmd = sql_con.CreateCommand();
            sql_cmd.CommandType = CommandType.Text;
            sql_cmd.CommandText = "select * from temp";



            SQLiteDataAdapter da = new SQLiteDataAdapter(sql_cmd);
            DB.Fill(DS);
            hytherm myReport = new hytherm();
            myReport.SetDataSource(DS);
            crystalReportViewer1.ReportSource = myReport;
            sql_con.Close();

            sql_con.Open();
            sql_cmd = sql_con.CreateCommand();
            sql_cmd.CommandType = CommandType.Text;
            sql_cmd.CommandText = "DELETE FROM table temp";
            sql_cmd.ExecuteNonQuery();
            sql_con.Close();
        }
    }
}

推荐答案

1。为什么要在select命令上执行cmd.ExecuteNonQuery?它执行命令,但对返回的数据不执行任何操作。

2. SQLite没有TRUNCATE命令。在没有WHERE子句的情况下执行DELETE FROM命令而不是TRUNCATE。 SQLite已经过优化,可以将其视为截断。

3. SQLite不对主模式使用dbo。如果要包含模式名称(可选),请使用main。
1. Why are you doing the cmd.ExecuteNonQuery on the select command? It executes the command, but does nothing with the data that is returned.
2. SQLite does not have a TRUNCATE command. Instead of TRUNCATE, do a "DELETE FROM" command without a WHERE clause. SQLite has been optimised to treat that as a truncate.
3. SQLite does not use the "dbo" for the main schema. If you want to include the schema name (which is optional), use "main".


这篇关于Sqlite不支持的文件格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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