SQL 服务器 CE 不支持使用 EF 执行 SQL 命令?

SQL Server CE not supporting ExecuteSQLCommand with EF?

我一直在使用 SQL Server Express LocalDb 和 Entity Framework 使用 VS2015 开发一个 C# WPF 项目。我为数据库构建了一个自定义播种器,它从 Excel 文件中读取测试数据,它只是将 Excel 数据组合成命令字符串,然后使用 context.Database.ExecuteSQLCommand 插入。

现在,我正考虑使用 SQL Server Compact Edition 4.0 启动项目,但我发现此命令不再有效。我是否必须使用 SqlCeConnectionSqlCeCommand 再次编写我的上传程序,或者我是否遗漏了什么?

此外,从某个地方我了解到,使用 EF 可以切换 SQL 提供程序,并且代码不需要其他更改。我会在路上遇到更多惊喜吗?

上传命令示例:

string cmd = "INSERT INTO Venues(Name, City, Telephone) Values ('X','Y','Z')"
context.Database.ExecuteSqlCommand(cmd);

错误:

There was an error parsing the query. [ Token line number = 2,Token line offset = 1,Token in error = INSERT ]

这不仅仅是一个测试问题,因为我也想在生产版本中包含这个上传器,以便快速插入主数据(例如员工列表)。

编辑:上传程序代码。如果这可以在不使用原始 SQL 的情况下完成,那也是一个很好的解决方案。

这遍历 Excel 工作表(以实体命名)和列(第一行具有 属性 名称)和第 2->n 行(数据)。这基本上可以处理 Excel 限制范围内的任何数据量的上传。关键是代码不知道实体(也可能参数化 DataContext)。代码可能不是最佳的,因为我只是一个初学者,但对我有用,除了 SQL CE。编辑以适应 CE 不是什么大问题,但我想寻求更好的方法。

public static class ExcelUploader
{
    static ArrayList data;
    static List<string> tableNames;

    public static string Upload(string filePath)
    {
        string result = "";
        data = new ArrayList();
        tableNames = new List<string>();
        ArrayList upLoadData = ReadFile(filePath);
        List<string> dataList = ArrayListToStringList(upLoadData);

        using (var db = new DataContext())
        {
            using (var trans = db.Database.BeginTransaction())
            {
                try
                {
                    foreach (var cmd in dataList)
                    {
                        Console.WriteLine(cmd);
                        db.Database.ExecuteSqlCommand(cmd);
                    }
                    db.SaveChanges();
                    trans.Commit();
                }
                catch (Exception e)
                {
                    trans.Rollback();
                    result = e.Message;
                    MessageBox.Show(result);
                }
            }
        }
        return result;
    }


    private static ArrayList ReadFile(string fileName)
    {
        List<string> commands = new List<string>();

        var xlApp = new Microsoft.Office.Interop.Excel.Application();
        var wb = xlApp.Workbooks.Open(fileName, ReadOnly: true);
        xlApp.Visible = false;
        foreach (Worksheet ws in wb.Worksheets)
        {
            var r = ws.UsedRange;
            var array = r.Value;
            data.Add(array);
            tableNames.Add(ws.Name);
        }
        wb.Close(SaveChanges: false);
        xlApp.Quit();

        return data;
    }

    private static List<string> ArrayListToStringList(ArrayList arrList)
    {
        List<string> result = new List<string>();

        for(int tableAmount = 0;tableAmount<data.Count;tableAmount++)
        {
            result.Add(ArrayToSqlCommand(arrList[tableAmount] as Array, tableNames[tableAmount]));
        }

        return result;
    }

    private static string ArrayToSqlCommand(Array arr, string tableName)
    {
        int propertyRow = 1;
        int firstDataRow = 2;
        string command = "";

        // loop rows                
        for (int rowIndex = firstDataRow; rowIndex <= arr.GetUpperBound(0); rowIndex++)
        {
            command += "INSERT INTO " + tableName + "(";

            //add column names
            for (int colIndex = 1; colIndex <= arr.GetUpperBound(1); colIndex++)
            {
                //get property name
                command += arr.GetValue(propertyRow, colIndex);

                //add comma if not last column, otherwise close bracket
                if (colIndex == arr.GetUpperBound(1))
                {
                    command += ") Values (";
                }
                else
                {
                    command += ", ";
                }
            }

            //add values
            for (int colIndex = 1; colIndex <= arr.GetUpperBound(1); colIndex++)
            {
                //get property value
                command += "'" + arr.GetValue(rowIndex, colIndex) + "'";

                //add comma if not last column, otherwise close bracket
                if (colIndex == arr.GetUpperBound(1))
                {
                    command += ");";
                }
                else
                {
                    command += ", ";
                }
            }
            command += "\n";
        }

        return command;
    }

}

有两种方法可以使用我提供的 SQL 原始查询。

初始数据

1) Excel table

+=======+=======+===========+
| Name  | City  | Telephone |
|===========================|
| Adam  | Addr1 | 111-11-11 |
|-------|-------|-----------|
| Peter | Addr2 | 222-22-22 |
+-------+-------+-----------+

2) SQL 服务器 CE table

CREATE TABLE Venues
(
    Id        int identity primary key,
    [Name]    nvarchar(100) null,
    City      nvarchar(100) null,
    Telephone nvarchar(100) null
);

3) 从 Excel

获取数据

这里我们感兴趣的是从Excelsheet获取数组。一拿到手,我们就可以安心的关闭了Excel。该代码假定文件 "Employees.xlsx" 位于 executable 文件的旁边。

private object[,] GetExcelData()
{
    xlApp = new Excel.Application { Visible = false };
    var xlBook =
        xlApp.Workbooks.Open(System.IO.Path.Combine(
                                 Environment.CurrentDirectory,
                                 "Employees.xlsx"));
    var xlSheet = xlBook.Sheets[1] as Excel.Worksheet;

    // For process termination
    var xlHwnd = new IntPtr(xlApp.Hwnd);
    var xlProc = Process.GetProcesses()
                 .Where(p => p.MainWindowHandle == xlHwnd)
                 .First();

    // Get Excel data: it's 2-D array with lower bounds as 1.
    object[,] arr = xlSheet.Range["A1"].CurrentRegion.Value;

    // Shutdown Excel
    xlBook.Close();
    xlApp.Quit();
    xlProc.Kill();
    GC.Collect();
    GC.WaitForFullGCComplete();

    return arr;
}

现在您可以使用其中一种方式生成查询。

选项 1. 使用 ExecuteSqlCommand

使用ExecuteSqlCommand时,建议使用参数化查询以避免错误。您可以显式传递 created SqlCeParameter 或仅传递一个值。

private void UseExecuteSqlCommand()
{
    object[,] arr = GetExcelData();

    using (var db = new EmpContext())
    {

        db.Database.Initialize(true);

        int count = 0;
        string sql = "INSERT INTO Venues (Name, City, Telephone) " +
                     "VALUES (@name, @city, @phone);";

        // Start from 2-nd row since we need to skip header
        for (int r = 2; r <= arr.GetUpperBound(0); ++r)
        {
            db.Database.ExecuteSqlCommand(
                sql,
                new SqlCeParameter("@name", (string)arr[r, 1]),
                new SqlCeParameter("@city", (string)arr[r, 2]),
                new SqlCeParameter("@phone", (string)arr[r, 3])
            );

            ++count;
        }

        conn.Close();
        MessageBox.Show($"{count} records were saved.");
    }
}

选项 2. 使用 DbConnection

如果您希望您的代码更通用,您可以创建接受 DbConnection 的方法。这将允许传递 SqlConnectionSqlCeConnection。但是代码变得更加冗长,因为我们不能使用构造函数,因为这些 类 是抽象的。

private void UseDbConnection()
{
    object[,] arr = GetExcelData();

    using (var db = new EmpContext())
    {

        db.Database.Initialize(true);

        int count = 0;
        string sql = "INSERT INTO Venues (Name, City, Telephone) " +
                     "VALUES (@name, @city, @phone);";

        DbParameter param = null;

        DbConnection conn = db.Database.Connection;
        conn.Open();

        DbCommand command = conn.CreateCommand();
        command.CommandText = sql;
        command.CommandType = CommandType.Text;

        // Create parameters

        // Name
        param = command.CreateParameter();
        param.ParameterName = "@name";
        command.Parameters.Add(param);

        // City
        param = command.CreateParameter();
        param.ParameterName = "@city";
        command.Parameters.Add(param);

        // Telephone
        param = command.CreateParameter();
        param.ParameterName = "@phone";
        command.Parameters.Add(param);

        // Start from 2-nd row since we need to skip header
        for (int r = 2; r <= arr.GetUpperBound(0); ++r)
        {
            command.Parameters["@name"].Value = (string)arr[r, 1];
            command.Parameters["@city"].Value = (string)arr[r, 2];
            command.Parameters["@phone"].Value = (string)arr[r, 3];
            command.ExecuteNonQuery();
            ++count;
        }

        conn.Close();
        MessageBox.Show($"{count} records were saved.");
    }
}

您还可以对参数使用序号位置,这样就无需创建参数名称并使代码更短:

private void UseDbConnection()
{

    object[,] arr = GetExcelData();

    using (var db = new EmpContext())
    {

        db.Database.Initialize(true);

        int count = 0;
        // Take a note - use '?' as parameters
        string sql = "INSERT INTO Venues (Name, City, Telephone) " +
                     "VALUES (?, ?, ?);";

        DbConnection conn = db.Database.Connection;
        conn.Open();
        DbCommand command = conn.CreateCommand();
        command.CommandText = sql;
        command.CommandType = CommandType.Text;

        // Create parameters
        command.Parameters.Add(command.CreateParameter());
        command.Parameters.Add(command.CreateParameter());
        command.Parameters.Add(command.CreateParameter());

        for (int r = 2; r <= arr.GetUpperBound(0); ++r)
        {
            // Access parameters by position
            command.Parameters[0].Value = (string)arr[r, 1];
            command.Parameters[1].Value = (string)arr[r, 2];
            command.Parameters[2].Value = (string)arr[r, 3];
            command.ExecuteNonQuery();
            ++count;
        }

        conn.Close();
        MessageBox.Show($"{count} records were saved.");
    }
}

P.S. 我没有检查底层连接是否打开,但这样做是个好主意。

基于 JohnyL 的出色输入,我能够修改我的代码,使其适用于 SQL Server Express 和 SQL Server CE。我将把我的新代码作为答案,因为我必须进一步参数化它,因为我也不能在代码中写 属性 名称。但这是一个简单的步骤,一旦我从 JohnyL 那里得到灵感。虽然不确定是否应该将数据库写入操作包装在 DbTransaction 中,但这暂时有效。

public static class ExcelUploader
{
    static ArrayList data;
    static List<string> tableNames;
    static List<DbCommand> cmdList = new List<DbCommand>();
    static DbConnection conn;

    public static void Upload(string filePath)
    {
        data = new ArrayList();
        tableNames = new List<string>();
        //get Excel data to array list
        ArrayList upLoadData = ReadFile(filePath);

        using (var db = new DataContext())
        {
            conn = db.Database.Connection;

            //transform arraylist into a list of DbCommands
            ArrayListToCommandList(upLoadData);

            conn.Open();
            try
            {
                foreach (var cmd in cmdList)
                {
                    //Console.WriteLine(cmd.CommandText);
                    cmd.ExecuteNonQuery();
                }
            }
            catch (Exception e)
            {
                var result = e.Message;
                MessageBox.Show(result);
            }
        }

    }

    //opens Excel file and reads worksheets to arraylist
    private static ArrayList ReadFile(string fileName)
    {
        List<string> commands = new List<string>();

        var xlApp = new Microsoft.Office.Interop.Excel.Application();
        var wb = xlApp.Workbooks.Open(fileName, ReadOnly: true);
        xlApp.Visible = false;
        foreach (Worksheet ws in wb.Worksheets)
        {
            var r = ws.UsedRange;
            var array = r.Value;
            data.Add(array);
            tableNames.Add(ws.Name);
        }
        wb.Close(SaveChanges: false);
        xlApp.Quit();

        return data;
    }

    //transforms arraylist to a list of DbCommands
    private static void ArrayListToCommandList(ArrayList arrList)
    {
        List<DbCommand> result = new List<DbCommand>();

        for (int tableAmount = 0; tableAmount < data.Count; tableAmount++)
        {

            ArrayToSqlCommands(arrList[tableAmount] as Array, tableNames[tableAmount]);
        }

    }

    private static void ArrayToSqlCommands(Array arr, string tableName)
    {
        //Excel row which holds property names
        int propertyRow = 1;
        //First Excel row with values
        int firstDataRow = 2;
        string sql = "";

        DbCommand cmd = conn.CreateCommand();

        sql += "INSERT INTO " + tableName + "(";

        //add column names to command text
        for (int colIndex = 1; colIndex <= arr.GetUpperBound(1); colIndex++)
        {
            //get property name
            sql += arr.GetValue(propertyRow, colIndex);

            //add comma if not last column, otherwise close bracket
            if (colIndex == arr.GetUpperBound(1))
            {
                sql += ") Values (";
            }
            else
            {
                sql += ", ";
            }
        }

        //add value parameter names to command text
        for (int colIndex = 1; colIndex <= arr.GetUpperBound(1); colIndex++)
        {
            //get property name
            sql += "@" + arr.GetValue(propertyRow, colIndex);

            //add comma if not last column, otherwise close bracket
            if (colIndex == arr.GetUpperBound(1))
            {
                sql += ");";
            }
            else
            {
                sql += ", ";
            }
        }

        //add data elements as command parameter values
        for (int rowIndex = firstDataRow; rowIndex <= arr.GetUpperBound(0); rowIndex++)
        {
            //initialize command
            cmd = conn.CreateCommand();
            cmd.CommandText = sql;
            cmd.CommandType = CommandType.Text;

            for (int colIndex = 1; colIndex <= arr.GetUpperBound(1); colIndex++)
            {
                //set parameter values
                DbParameter param = null;
                param = cmd.CreateParameter();
                param.ParameterName = "@" + (string)arr.GetValue(propertyRow, colIndex);
                cmd.Parameters.Add(param);
                cmd.Parameters[param.ParameterName].Value = arr.GetValue(rowIndex, colIndex);
            }
            //add command to command list
            cmdList.Add(cmd);

        }

    }
}