存储过程使用(数据库新增) 1. SQL数据库存储过程代码 CREATE PROCEDURE Adddaily ( @username VarChar(50) , @name VarChar(50) , @dept VarChar(50) , @content ntext , @finish VarChar(50) , @state VarChar(4) , @data DateTime , @up_date DateTime ) AS insert into Daily (username,name,dept,content,finish,state,data,up_date)values(@username,@name,@dept,@content,@finish,@state,@data,@up_date) GO
2. .NET后台代码 protected void Button2_Click(object sender, EventArgs e) { //存储过程实例--添加记录 //从配置文件获取数据库连接串 Conn conn = new Conn(); SqlConnection con = conn.getConn(); DateTime dt = DateTime.Now; SqlCommand cmd = new SqlCommand("Adddaily", con); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add("@username", SqlDbType.VarChar, 50).Value = usernameLabel.Text; cmd.Parameters.Add("@name", SqlDbType.VarChar, 50).Value = nameLabel.Text; cmd.Parameters.Add("@dept", SqlDbType.VarChar, 50).Value = deptLabel.Text; cmd.Parameters.Add("@content", SqlDbType.Text, 16).Value = content.Text; cmd.Parameters.Add("@finish", SqlDbType.VarChar, 50).Value = finish.Text; cmd.Parameters.Add("@state", SqlDbType.VarChar, 4).Value = state.Text; cmd.Parameters.Add("@data", SqlDbType.DateTime, 4).Value = data.Text; cmd.Parameters.Add("@up_date", SqlDbType.DateTime, 4).Value = dt; try { cmd.ExecuteNonQuery(); } catch (Exception err) { throw new System.Exception(err.Message); } finally { BindData(); } }
存储过程使用(数据库修改) 1. SQL数据库存储过程代码 CREATE PROCEDURE Updatedaily ( @username VarChar(50) , @name VarChar(50) , @dept VarChar(50) , @content ntext , @finish VarChar(50) , @state VarChar(4) , @data DateTime , @id int ) AS update Daily set username=@username,name=@name,dept=@dept,content=@content,finish=@finish,state=@state,data=@data where ID=@ID GO
2. .NET后台代码 protected void Button1_Click(object sender, EventArgs e) { Conn conn = new Conn(); SqlConnection con = conn.getConn(); SqlCommand cmd = new SqlCommand("Updatedaily", con); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add("@username", SqlDbType.VarChar, 50).Value = usernameLabel.Text; cmd.Parameters.Add("@name", SqlDbType.VarChar, 50).Value = nameLabel.Text; cmd.Parameters.Add("@dept", SqlDbType.VarChar, 50).Value = deptLabel.Text; cmd.Parameters.Add("@content", SqlDbType.Text, 16).Value = content.Text; cmd.Parameters.Add("@finish", SqlDbType.VarChar, 50).Value = finish.Text; cmd.Parameters.Add("@state", SqlDbType.VarChar, 4).Value = state.Text; cmd.Parameters.Add("@data", SqlDbType.DateTime, 4).Value = data.Text; cmd.Parameters.Add("@id", SqlDbType.Int).Value = ID.Text; try { cmd.ExecuteNonQuery(); } catch (Exception err) { throw new System.Exception(err.Message); } finally { ID.Text = ""; finish.Text = ""; content.Text = ""; state.Text = ""; BindData(); }
|