利用存储过程往数据库中插入记录
来源:互联网 发布:桥接模式下centos联网 编辑:程序博客网 时间:2024/06/05 03:21
数据库存储过程代码
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" onclick="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());
}
}
}
}
}
- 利用存储过程往数据库中插入记录
- 利用存储过程读指定文件夹里所有txt文件,将数据插入数据库中
- 利用mysql存储过程向数据库批量插入数据
- 利用oracle存储过程向数据库批量插入数据
- 利用PreparedStatement对象实现在数据库中插入一条记录。
- proc 存储过程,通过存储过程往数据库的某个表中插入指定数据量的纪录,表中有primary key
- 使用存储过程向Oracle数据库中插入BLOB文件
- 利用存储过程来消除数据库中冗余的数据
- 查询数据库中最近30天记录总数存储过程
- mysql利用存储过程插入测试数据
- mysql利用存储过程批量插入数据
- 利用存储过程插入50W+数据
- oracle 存储过程循环插入10000条记录---存储过程
- oracle 存储过程循环插入10000条记录---存储过程
- oracle 存储过程 利用数组类型type实现批量插入数据库
- 利用数据库的存储过程批量建表和批量插入数据
- 利用 notepad++快速插入数据库测试记录
- 利用存储过程实现记录集循环
- 重新认识一下wince的内存映射表!怀疑内存映射表最大就是512M,真是.......郁闷之极!
- 深入浅出MFC文档/视图架构之相互关系
- python 系统学习笔记(十一)---sys
- 直接定位vector中间元素的方法
- 使用libdwraf写的一个读取GCC -g参数编译生成的调试信息的例子
- 利用存储过程往数据库中插入记录
- 网络连接
- 存储过程
- C#矩阵类运算
- Android的底层库libutils介绍
- hdu 3792 Twin Prime Conjecture
- 在虚拟机linux下如何上网
- 基于Video4linux的视频采集,用SDL显示
- sql中的存储过程