大家好,我得到了cities
表,有4列cityId
是“AUTO_INCRMENT”,cityName
,governate
,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/