mysql之添加函数
数据库:
create PROCEDURE helloworld(in age int(10))
BEGIN
DECLARE customer_count INT(10);
select count(*) into customer_count from customer where customer_age>age;
if(customer_count>0) then
SELECT customer_count;
END IF;
END
c#调用数据库:
public class Produce
{
private MySqlConnection dbConnection;
public void Fun()
{
int age = 10;
DataSet ds = new DataSet();
//设置
MySqlDataAdapter adapter = new MySqlDataAdapter("helloworld", dbConnection);
adapter.SelectCommand.CommandType = System.Data.CommandType.StoredProcedure;
//设置参数
MySqlParameter ageParam = new MySqlParameter("?age", MySqlDbType.UInt32, 10);
ageParam.Value = age;
adapter.SelectCommand.Parameters.Add(ageParam);
//调用数据库函数,返回结果
adapter.Fill(ds, "helloworld");
DataTable dt = ds.Tables[0];
string result = dt.Rows[0][0].ToString();
}
}
浙公网安备 33010602011771号