0

我按照此处的说明尝试从 LightSwitch 2012 应用程序中调用(无参数)MySQL SP。此引用的说明适用于 SQL Server SP。

以下是相关代码:

using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;

namespace LightSwitchApplication
{
    public partial class StoredProcceduresService
    {
        partial void MakeMasterOperations_Inserting(MakeMasterOperation entity)
        {
            using (SqlConnection connection = new SqlConnection())
            {
                string connectionStringName = this.DataWorkspace.SystemInfo.Details.Name;
                connection.ConnectionString = ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString;

                string storedProcedure = "make_master";
                using (SqlCommand command = new SqlCommand(storedProcedure, connection))
                {
                    command.CommandType = CommandType.StoredProcedure;
                    connection.Open();
                    command.ExecuteNonQuery();
                }            
            }
        this.Details.DiscardChanges();
        }
    }
}

这将失败connection.Open();并出现 SqlException“用户‘root’登录失败。” 我知道用户名和密码都可以,因为在 LightSwitch 中使用相同连接字符串的其他数据库操作也可以正常工作。

是否可以在 LightSwitch 中调用 MySQL SP?如果是这样,怎么做?

4

1 回答 1

0

嗯,我找到了答案。

为了使用 MySQL 而不是 SQL Server,必须在服务器项目中添加对 MySql.Data 的引用并将代码更改为:

    using System;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using MySql.Data.MySqlClient;

    namespace LightSwitchApplication
    {
        public partial class StoredProcceduresService
        {
            partial void MakeMasterOperations_Inserting(MakeMasterOperation entity)
            {
                using (MySqlConnection connection = new MySqlConnection())
                {
                    string connectionStringName = this.DataWorkspace.SystemInfo.Details.Name;
                    connection.ConnectionString = ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString;

                    string storedProcedure = "make_master";
                    using (MySqlCommand command = new MySqlCommand(storedProcedure, connection))
                    {
                        command.CommandType = CommandType.StoredProcedure;
                        connection.Open();
                        command.ExecuteNonQuery();
                    }            
                }
            this.Details.DiscardChanges();
            }
        }
    }

太糟糕了,似乎没有一个通用的基类允许根据使用的数据库服务器进行实例化..

于 2013-10-01T11:58:53.713 回答