废话不说,直接切入主题。先来说下存储过程的几种情况:
1、没有参数没有返回值
2、有参数没有返回值
3、有参数有返回值
下面就这几种情况分别举例:1、没有参数没有返回值 象这种情况最简单。
-
存储过程
- USE Northwind
- CREATE PROC novaluenoparameter
- AS
- SELECT * FROM products
- GO
- /// <summary>
- /// c#代码 方法1
- /// </summary>
- SqlConnection conn = new SqlConnection();
- conn.ConnectionString = strConn;
- conn.Open();
- SqlCommand comm = new SqlCommand("EXEC novaluenoparameter", conn);
- comm.ExecuteNonQuery();
- conn.Close();
情况2、有参数没有返回值
- 存储过程(带参数,没有返回值)
- CREATE PROC novaluebeparameter
- @i int ,
- @productname varchar(20)
- AS
- SELECT TOP @i * FROM products where productname = @productname
- GO
- /// <summary>
- /// C#代码:调用带参数没有返回值的存储过程
- /// </summary>
-
- SqlConnection conn = new SqlConnection();
- conn.ConnectionString = strConn;
- conn.Open();
- SqlCommand comm = new SqlCommand("novaluebeparameter", conn); //“novaluebeparameter”是存储过程名
- comm.CommandType = CommandType.StoredProcedure;
- comm.Parameters.Add(new SqlParameter("@i",SqlDbType.Int));
- comm.Parameters.Add(new SqlParameter("@productname",SqlDbType.varchar,20));
- comm.Parameters["@i"].Value=3;
- comm.Parameters["@productname"].value = "Tofu"
- comm.ExecuteNonQuery();
- conn.Close();
3、带参数,有返回值(返回一个值,这里说下,有返回一个值的,有返回一个数据集的。)
- 存储过程(带参数,没有返回值)
- CREATE PROC novaluebeparameter
- @i int ,
- @j int,
- @sum int output
- AS
- SET @sum = @i + @j
- RETURN @sum
- GO