发表评论
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;
namespace Mirror.AutoWeb.HelpCSharp.DataBase
{
/// <summary>
/// StudyParameters 的摘要说明。
/// </summary>
public class StudyParameters : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox RoleName;
protected System.Web.UI.WebControls.TextBox Description;
protected System.Web.UI.WebControls.Button SureBtn;
protected System.Web.UI.WebControls.Literal SuccessMessage;
private readonly string connstr = ConfigurationSettings.AppSettings["MySQLConnString1"].ToString();
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.SureBtn.Click += new System.EventHandler(this.SureBtn_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void SureBtn_Click(object sender, System.EventArgs e)
{
if(RoleName.Text.Trim().Length>0)
{
int roleID;
//如果有重复的角色,则使用存储过程判断并不增加角色;
roleID=AddRole(RoleName.Text.Trim(),Description.Text.Trim());
SuccessMessage.Visible=true;
SuccessMessage.Text="添加角色成功!角色编号:" + roleID.ToString();
}
else
{
SuccessMessage.Visible=true;
SuccessMessage.Text="添加角色失败!";
}
}
private int AddRole(string sRoleName,string sRoleDescn)
{
SqlConnection MyConn = new SqlConnection(connstr);
SqlCommand MyCmd = new SqlCommand("Pr_AddRole",MyConn);
MyCmd.CommandType = CommandType.StoredProcedure;
SqlParameter parameterRoleName = new SqlParameter("@RoleName",SqlDbType.VarChar);
parameterRoleName.Value=sRoleName;
MyCmd.Parameters.Add(parameterRoleName);
MyCmd.Parameters.Add(new SqlParameter("@Description",SqlDbType.Text));
MyCmd.Parameters[1].Value=sRoleDescn;
SqlParameter parameterRoleID = new SqlParameter("@Role_ID",SqlDbType.Int);
parameterRoleID.Direction = ParameterDirection.ReturnValue;
MyCmd.Parameters.Add(parameterRoleID);
MyConn.Open();
MyCmd.ExecuteNonQuery();
MyConn.Close();
return (int)parameterRoleID.Value;
}
}
#region "SQL建立命令"
/*
create Procedure Pr_AddRole
(
@RoleName varchar(255),
@Description text
)
as
declare @RecordCount as int
Set @RecordCount =
(
Select count(*) From Role Where Role_Name= @RoleName
)
if @RecordCount<1
Begin
Insert into Role(Role_Name,Role_Description)
values
(@RoleName,@Description)
return @@Identity
End
GO
* */
/*
* if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Role]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[Role]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[StaffRole]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[StaffRole]
GO
CREATE TABLE [dbo].[Role] (
[Role_ID] [int] IDENTITY (1, 1) NOT NULL ,
[Role_Name] [varchar] (255) COLLATE Chinese_PRC_CI_AS NULL ,
[Role_Description] [text] COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
CREATE TABLE [dbo].[StaffRole] (
[Staff_ID] [int] NULL ,
[Role_ID] [int] NULL
) ON [PRIMARY]
GO
* */
#endregion
}
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。