您的位置:首页 > 数据库

SQL serve创建与调用存储过程

2016-05-04 16:10 302 查看

(1)创建



2编写存储过程(创建传参的存储过程)存储过程语法网络上很多不在累述



语法解析
Use Person  指定在那个数据库下建立存储过程
if (object_id('MyFunction', 'P') is not null) 用于避免创建相同的存储过程
drop proc MyFunction
GO
create proc MyFunction(@name varchar(50),@newsid int)  创建带参的函数
as
begin
Update Info set name = @name where id = @newsid
End
exec MyFunction "王明洋",2     用于测试存储过程 http://www.cnblogs.com/hoojo/archive/2011/07/19/2110862.html http://www.cnblogs.com/sosoft/p/3535696.html


  

(2)

C#调用

int ClassID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());

string CName = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString();

string sqlStr = "update Info set name='" + CName + "' where id=" + ClassID;

SqlConnection myConn = GetConnection();

myConn.Open();

SqlCommand myCmd = new SqlCommand("MyFunction", myConn);

myCmd.CommandType = CommandType.StoredProcedure;//开启调用存储过程

myCmd.Parameters.Add("@name", SqlDbType.VarChar, 50).Value = CName;//存储过程所需的参数

myCmd.Parameters.Add("@newsid", SqlDbType.Int).Value = ClassID;//存储过程所需的参数

myCmd.ExecuteNonQuery();

myCmd.Dispose();

myConn.Close();

GridView1.EditIndex = -1;

this.bind();


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: