c# - 使用C Sharp三层架构MVC将数据插入mysql数据库(MysqlParameter插入空值)

标签 c# mysql model-view-controller

大家好,我得到了cities表,有4列cityId是“AUTO_INCRMENT”,cityNamegovernate ,conId 是对countries

的外键引用

它们是使用last_insert_id()

将数据插入此表的存储过程
CREATE DEFINER=`root`@`localhost` PROCEDURE `addCity`(in  cityNamee varchar(45),bGovernate varchar(45),cconId int(5),out ccityId int(5))BEGIN insert into cities(`cityName`,`governate`,`conId`)values (@cityNamee,@bGovernate,@cconId);set @ccityId=last_insert_id();END

从业务层( Controller )调用 sp 并将 throw 传递给模型,然后执行过程,这里是传递插入、更新、删除命令的模型

public void Transfare(string _chanPoro, MySqlParameter[] _dataContainer)
    {
        MySqlCommand transmeter = new MySqlCommand
        {
            CommandType = CommandType.StoredProcedure,
            CommandText = _chanPoro,
            Connection = dbcon
        };

        transmeter.Parameters.AddRange(_dataContainer);
        connect();
        transmeter.ExecuteNonQuery();
        disConnect();
    }

我尝试使用 MySqlParameter 插入数据,但总是失败并给出错误

 public static void insertCity(string cityName, string governate, int countryId)
    {
        try
        {
                _socket = new _ctrlChannel();

                MySqlParameter[] zair = new MySqlParameter[4];


                zair[0] = new MySqlParameter("@cityNamee", MySqlDbType.VarChar, 45)
                { Value = cityName, Direction = ParameterDirection.Input
                };

                zair[1] = new MySqlParameter("@bGovernate", MySqlDbType.VarChar)
                { Value = governate, Direction = ParameterDirection.Input
                };

                zair[2] = new MySqlParameter("@cconId", MySqlDbType.VarChar)
                { Value = countryId, Direction = ParameterDirection.Input
                };

                zair[3] = new MySqlParameter("@ccityId", MySqlDbType.Int32,5)
                {  Direction = ParameterDirection.Output
                };

                _socket.connect();
                _socket.Transfare("addCity", zair);
                _socket.disConnect();
        }
        catch (MySql.Data.MySqlClient.MySqlException we) { MessageBox.Show("error" + we); }
    }

cityName was not found in the collection

我尝试使用传统方式用此方法插入数据,这里的主要问题是将空值插入数据库(不起作用),而表设计不支持此操作

  public static void insertNewCity(string cityName, string governate, int countryId)
    {
        try
        {
            _socket = new _ctrlChannel();

            _ctrlChannel.InitializeDb();
            MySqlCommand cmd = new MySqlCommand();
            cmd.Connection = _ctrlChannel.dbcon;
            cmd.CommandText = "addCity";
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.AddWithValue("@cityNamee", cityName);
            cmd.Parameters["@cityNamee"].Direction = ParameterDirection.Input;

            cmd.Parameters.AddWithValue("@bGovernate", governate);
            cmd.Parameters["@bGovernate"].Direction = ParameterDirection.Input;

            cmd.Parameters.AddWithValue("@cconId", countryId);
            cmd.Parameters["@cconId"].Direction = ParameterDirection.Input;

            cmd.Parameters.AddWithValue("@ccityId", MySqlDbType.Int32);
            cmd.Parameters["@ccityId"].Direction = ParameterDirection.Output;
            _socket.connect();

            cmd.ExecuteNonQuery();

            _socket.disConnect();
        }
        catch (MySql.Data.MySqlClient.MySqlException we) { MessageBox.Show("error" + we); }
    }

Note : The procedure is full working when i tested it on server

这就是全部,非常感谢...

最佳答案

确保有 getter 和 setter 将 View 中的值保存到业务层,然后业务层传递到 dataAccessLayer 将其插入数据库

    string _cityName;
    string _governate;
    int _countryId;
    private string cityName { get; set; }
    private string governate { get; set; }
    private int countryId { get; set; }

    public void prepare()
    {
        _cityName = cityName;
        _governate = governate;
        _countryId = countryId;
    }
    public  void insertCity( string _cityName ,string _governate, int _countryId)
    {
        try
        {
                _socket = new _ctrlChannel();

                MySqlParameter[] zair = new MySqlParameter[4];


                zair[0] = new MySqlParameter("@cityNamee", MySqlDbType.VarChar, 45)
                { Value = _cityName, Direction = ParameterDirection.Input
                };

                zair[1] = new MySqlParameter("@bGovernate", MySqlDbType.VarChar)
                { Value = _governate, Direction = ParameterDirection.Input
                };

                zair[2] = new MySqlParameter("@cconId", MySqlDbType.VarChar)
                { Value = _countryId, Direction = ParameterDirection.Input
                };

                zair[3] = new MySqlParameter("@ccityId", MySqlDbType.Int32,5)
                {  Direction = ParameterDirection.Output,Value=DBNull.Value
                };

            _socket.connect();
            _socket.Transfare("addCity", zair);
            _socket.disConnect();
        }
        catch (MySql.Data.MySqlClient.MySqlException we) { MessageBox.Show("error" + we); }
    }

关于c# - 使用C Sharp三层架构MVC将数据插入mysql数据库(MysqlParameter插入空值),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53662366/

相关文章:

C#:在 SelectedIndexChange 事件处理程序中设置 ComboBox 文本值?

C# - 允许继承但禁止直接使用构造函数

php - 在你的 Controller 层进行内容协商可以吗?

jquery - 在 angularJS Controller 中使用 jQuery 的 $.ajax

ios - Swift - 使用跨多个 View 的属性初始化模型对象

c# - 在 C# ASP .Net MVC 中编辑十进制模型属性

c# - 如何根据每个请求更改 JsonSerializerSettings

php - 从多个表中选择顺序和组

php - 如何使用 MySQL 以简单的增量更新 VARCHAR 值?

php - Twilio 短信 mysql 插入