【问题标题】:How to call MySQL Stored Procedures from Lightswitch?如何从 Lightswitch 调用 MySQL 存储过程?
【发布时间】:2013-10-07 12:13:41
【问题描述】:

我按照here 的说明尝试从 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' 登录失败。”我知道用户 ID 和密码都可以,因为在 LightSwitch 中使用相同连接字符串的其他数据库操作也可以正常工作。

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

【问题讨论】:

    标签: mysql sql sql-server visual-studio-lightswitch lightswitch-2012


    【解决方案1】:

    嗯,我找到了答案。

    为了使用 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();
                }
            }
        }
    

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

    【讨论】:

      猜你喜欢
      • 2019-02-20
      • 2010-09-09
      • 2012-12-30
      • 1970-01-01
      • 2011-05-05
      • 1970-01-01
      • 1970-01-01
      • 2014-09-07
      相关资源
      最近更新 更多