Creating database using DataContext and System.Data.SQLite in C#

最后都变了- 提交于 2019-12-07 17:46:57

问题


I'm writing simple application gathering information about machine's hardware. I plan to store data in sqlite. Got following code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SQLite;
using System.IO;
using System.Data;
using System.Data.Linq;
using System.Data.Linq.Mapping;

namespace SQLiteTest
{
    public class MyDB : DataContext
    {
        public Table<Computer> kompy;
        public MyDB(string connection) : base(connection) { }
        public MyDB(IDbConnection connection) : base(connection) { }
    }


    [Table]
    public class Computer
    {
        [Column(IsPrimaryKey = true, CanBeNull = false)]
        public uint CompId;

        [Column]
        public uint CompanyId;

        [Column]
        public string CompName;
    }


    class Program
    {
        static void Main(string[] args)
        {
            string rootPath = Environment.CurrentDirectory;
            string dbPath = rootPath + "\\db.sqlite3";

            SQLiteConnectionStringBuilder builder = new SQLiteConnectionStringBuilder();
            builder.Add("Data Source", dbPath);

            SQLiteConnection sqlcnn = new SQLiteConnection(builder.ConnectionString);
            MyDB db = new MyDB(sqlcnn);
            db.CreateDatabase();
            db.SubmitChanges();
            db.Dispose();            
        }
    }
}

Running this program produces exception:

Unhandled Exception: System.Data.SQLite.SQLiteException: SQLite error
near "DATABASE": syntax error
   at System.Data.SQLite.SQLite3.Prepare(SQLiteConnection cnn, String strSql, SQ
LiteStatement previous, UInt32 timeoutMS, String& strRemain)
   at System.Data.SQLite.SQLiteCommand.BuildNextCommand()
   at System.Data.SQLite.SQLiteCommand.GetStatement(Int32 index)
   at System.Data.SQLite.SQLiteDataReader.NextResult()
   at System.Data.SQLite.SQLiteDataReader..ctor(SQLiteCommand cmd, CommandBehavi
or behave)
   at System.Data.SQLite.SQLiteCommand.ExecuteReader(CommandBehavior behavior)
   at System.Data.SQLite.SQLiteCommand.ExecuteNonQuery()
   at System.Data.Linq.SqlClient.SqlProvider.ExecuteCommand(String command)
   at System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider
.CreateDatabase()
   at System.Data.Linq.DataContext.CreateDatabase()
   at SQLiteTest.Program.Main(String[] args) in C:\Users\bootch\documents\visual
 studio 2010\Projects\SQLiteTest\SQLiteTest\Program.cs:line 47

1) Why this code doesn't work, what am I missing?

2) Is there a way to get text version of underlying sql command to see what's wrong

3) If database doesn't exist I want create database and all tables. I'm lazy so I thought I can use types I created for running queries. Is it possible with System.Data.SQLite?

Thanks in advance, for comments and answers!


回答1:


Looks like you trying to use Linq2Sql with Sqlite which isn't supported by default (only SQL Server and SQL Server CE are supported), see this question for more details.

Have a look at using Entity Framework, which supports Sqlite, instead.




回答2:


I just went through this and tried to implement. Got same error, then found that we can create database using SQLiteConnection as so:

sqlcnn.CreateFile("MyDatabase.sqlite");

And then using "db" you can do all database operation. Try it.



来源:https://stackoverflow.com/questions/9470583/creating-database-using-datacontext-and-system-data-sqlite-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!