在C#中使用DataContext和System.Data.SQLite创建数据库 [英] Creating database using DataContext and System.Data.SQLite in C#

查看:747
本文介绍了在C#中使用DataContext和System.Data.SQLite创建数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写简单的应用程序,以收集有关机器硬件的信息.我计划将数据存储在sqlite中.得到以下代码:

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)为什么此代码不起作用,我丢失了什么?

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

2)有没有一种方法可以获取底层sql命令的文本版本,以查看出现了什么问题

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

3)如果数据库不存在,我想创建数据库和所有表.我很懒,所以我想我可以使用为运行查询创建的类型.可以使用System.Data.SQLite吗?

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!

推荐答案

就像您尝试将Linq2Sql与默认情况下不受支持的Sqlite一起使用(仅支持SQL Server和SQL Server CE),请参见此问题.

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.

使用支持Sqlite的实体框架 ,相反.

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

这篇关于在C#中使用DataContext和System.Data.SQLite创建数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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