大家好,我来了 cities
表有4列 cityId
是“自动增量”, 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
从业务层(控制器)调用sp,将throw传递给model,然后执行以下过程:model传递insert、update、delete命令
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
在集合中找不到
我尝试使用传统的插入数据的方法,这里的主要问题是在数据库中插入空值(不起作用),这在表设计中是不受支持的
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); }
}
注意:当我在服务器上测试它时,这个过程是完全正常的
就这些了非常感谢你。。。
1条答案
按热度按时间j2cgzkjk1#
确保getter和setter将视图中的值保存到业务层,然后业务层将其传递给
dataAccessLayer
将其插入数据库