求一段传递参数代码,还望大家不吝赐教.
目的是实现在添加新闻成功后,把刚才添加成功后的新闻的ID传到下一个页,让接受页以这个ID为条件把详细内容都查询出来.
数据库:
create database NewsTest
use NewsTest
create table News
(
NewsID int identity(1,1) primary key,
NewTitle varchar(100) not null,
NewsMaster varchar(500) not null,
)
数据库连接类:DB.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/// <summary>
/// DB 的摘要说明
/// </summary>
public class DB
{
public DB()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public static
System.Data.SqlClient.SqlConnection createCon()
{
return new System.Data.SqlClient.SqlConnection( "server=.;database=NewsTest;uid=sa;pwd=123; ");
}
}
定义要添加的类:type.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/// <summary>
/// type 的摘要说明
/// </summary>
public class type
{
public string NewsTitle;
public string NewsMaster;
public type()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
}
添加类:ADD.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
/// <summary>
/// ADD 的摘要说明
/// </summary>
public class ADD
{
public ADD()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public static bool insertOperate(type t)
{
try
{
SqlConnection con = DB.createCon();
con.Open();
SqlCommand cmd = new SqlCommand( "insert into News values(@NewsTitle,@NewsMaster) ", con);