1.创建存储过程test
create procedure test --创建存储过程test
as
BEGIN
insert into Wu
select name, age from [study-wu] where age < 20 --把查询到表study-wu中age<20的数据,插入到表Wu中
END
2.执行存储过程test,先在数据库查看结果
EXEC test --执行存储过程test
3.在winform程序中调用
AdoHelp ah = new AdoHelp();//实例化SQL辅助类
string ex = "EXEC test";//执行存储过程
int result = ah.ExecuteNonQuery(ex);
if (result > 0)
{
MessageBox.Show("数据插入成功");
}
else
{
MessageBox.Show("数据插入失败");
}
如想了解AdoHelp.cs请到:https://blog.csdn.net/kuguhuan/article/details/98599143
4.查看数据插入结果
AdoHelp ah = new AdoHelp();//实例化SQL辅助类
string strSql = "select * from Wu";//执行存储过程
DataSet ds = ah.ExecuteQueryDataSet(strSql);//查询Wu表,返回DataSet集合
dataGridView1.DataSource = ds.Tables[0];
注意:
存储过程test,在数据库中创建就好,之后只需要在程序中调用“exec test” ,不需要调用到之前编辑好的存储过程,很方便。