I want to call a Mysql stored procedure from c# . I am running the same procedure in mysql query browser finely . This is about getting a maturity date from given input year , month , days with current date . This is the output
DELIMITER $$
DROP PROCEDURE IF EXISTS `passbook`.`maturity_date` $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `maturity_date`(IN `stdate`
DATE, IN `adyear` INT(4), IN `amonth` INT(2), IN `adays` INT(2), INOUT
`matdate` DATE)
NO SQL
Begin
declare new_date , pstdate date;
set new_date = stdate;
if adyear > 0 then
SELECT date_add(stdate, INTERVAL adyear year) into pstdate;
set new_date = pstdate;
end if;
if amonth > 0 then
SELECT date_add(new_date, INTERVAL amonth MONTH) into pstdate;
set new_date = pstdate;
end if;
if adays > 0 then
SELECT date_add(new_date, INTERVAL adays DAY) into pstdate;
set new_date = pstdate;
end if;
Set matdate = new_date;
Select matdate;
end $$
DELIMITER ;
try this:
var ConnectionString="Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword";
var sqlParameters = new List<SqlParameter>();
var storedProcedureName = "SPName";
//add parameters with value and type here
sqlParameters.Add(new SqlParameter { SqlDbType = SqlDbType.Int, ParameterName = "@adyear", Value = adyear});
sqlParameters.Add(new SqlParameter { SqlDbType = SqlDbType.Int, ParameterName = "@amonth", Value = amonth});
sqlParameters.Add(new SqlParameter { SqlDbType = SqlDbType.Int, ParameterName = "@adys ", Value = adys });
//execute store procedure here
using (var connection = new SqlConnection(ConnectionString))
using (var command = new SqlCommand(storedProcedureName, connection) { CommandTimeout = 160, CommandType = commandType })
using (var dataAdaptor = new SqlDataAdapter(command))
{
command.Parameters.AddRange(sqlParameters);
connection.Open();
dataAdaptor.Fill(dS);
}
also you can use Mysql.Data
i believe it is the easiest way to do. You don't need anything else to work with mysql database.
PM> Install-Package MySql.Data
you can store the connection string in a file. just create a file and put your connection string on it. then read it in a static class. something like this:
CS = File.ReadAllText(YourDataBaseConfigFilePath);
then use it as your connection string.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.