数据库存储过程代码
CREATE PROC Pro_GetMaxId1
@newstitle VARCHAR(64),
@newscontent VARCHAR(max),
@newscreator VARCHAR(8),
@createtime DATETIME=NULL,
@classname VARCHAR(8),
@maxid INT OUT
AS
--根据类别名称查询出类别编号
DECLARE @classid uniqueidentifier;
SELECT @classid=T1.ClassId FROM T_NewsClass T1 WHERE T1.ClassName=@classname;
IF @createtime IS NULL
BEGIN
INSERT INTO T_News(NewsTitle,NewsContent,NewsCreator,CreateTime,ClassId)
VALUES
(@newstitle,@newscontent,@newscreator,GETDATE(),@classid);
END
ELSE
BEGIN
INSERT INTO T_News(NewsTitle,NewsContent,NewsCreator,CreateTime,ClassId)
VALUES
(@newstitle,@newscontent,@newscreator,@createtime,@classid);
END
SET @maxid=@@IDENTITY;
GO
--执行存储过程
DECLARE @maxid INT;
EXEC Pro_GetMaxId1 '222','222222','004',NULL,'军事',@maxid OUT;
PRINT @maxid;
GO
前台代码
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table style="width:40%;">
<tr>
<td>
新闻标题:</td>
<td>
<asp:TextBox ID="txtNewsTitle" runat="server" Width="300px"></asp:TextBox>
</td>
</tr>
<tr>
<td>
新闻内容:</td>
<td>
<asp:TextBox ID="txtNewsContent" runat="server" Width="300px" TextMode="MultiLine"
Height="155px"></asp:TextBox>
</td>
</tr>
<tr>
<td>
新闻类别:</td>
<td>
<asp:DropDownList ID="ddlClassName" runat="server">
</asp:DropDownList>
</td>
</tr>
<tr>
<td>
创建者:</td>
<td>
<asp:DropDownList ID="ddlNewsCreator" runat="server">
</asp:DropDownList>
</td>
</tr>
<tr><td colspan="2">
<asp:Calendar ID="Calendar1" runat="server" Width="213px"></asp:Calendar>
</td></tr>
<tr >
<td colspan="2" align="center">
<asp:Button ID="btnupdate" runat="server" Text="保存" style="height: 21px" οnclick="btnupdate_Click" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
后台代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
namespace 新闻系统
{
public partial class cunchuguocheng2 : System.Web.UI.Page
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
using (SqlConnection conn = new SqlConnection(constr))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "select ClassId,ClassName from T_NewsClass";
using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
adapter.Fill(dt);
ddlClassName.DataSource = dt;
ddlClassName.DataTextField = "ClassName";
ddlClassName.DataValueField = "ClassId";
ddlClassName.DataBind();
}
}
}
using (SqlConnection conn = new SqlConnection(constr))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "select UserId,RealName from T_User";
using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
adapter.Fill(dt);
ddlNewsCreator.DataSource = dt;
ddlNewsCreator.DataTextField = "RealName";
ddlNewsCreator.DataValueField = "UserId";
ddlNewsCreator.DataBind();
}
}
}
}
}
protected void btnupdate_Click(object sender, EventArgs e)
{
string newstitle = txtNewsTitle.Text;
string newscontent = txtNewsContent.Text;
string classid = ddlClassName.SelectedItem.Value;
string classname = ddlClassName.SelectedItem.Text;
using (SqlConnection conn = new SqlConnection(constr))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "Pro_GetMaxId1";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@maxid", SqlDbType.Int);
cmd.Parameters["@maxid"].Direction = ParameterDirection.Output;
cmd.Parameters.Add(new SqlParameter("@newstitle", newstitle));
cmd.Parameters.Add(new SqlParameter("@newscontent", newscontent));
cmd.Parameters.Add(new SqlParameter("@newscreator", userid));
cmd.Parameters.Add(new SqlParameter("@createtime", Calendar1.SelectedDate));
cmd.Parameters.Add(new SqlParameter("@classname", classname));
cmd.ExecuteNonQuery();
Response.Write(cmd.Parameters["@maxid"].Value.ToString());
}
}
}
}
}