我必须生成一个数字"参考编号"在c#中我将一行插入表中。
此参考编号应在插入数据后生成,并保存在表格的列中:
我的表(dbo.Cha)结构是:
ChaId (Primary Key) - 1
CreatedDate (Date) - 23/03/2016
ChaReferenceId (Varchar) -
最终结果将是"CHA0316001"
我想添加"表名+月/年+ 00 +主键" - CHA0316001 并将值保存在" ChaReferenceId"同一列的列 - dbo.cha
sql server - 过程:
CREATE PROCEDURE [dbo].[Cha_Insert]
@ChaName varchar(50),
@PortId int,
@StateId int = NULL,
@CountryId int,
@Address varchar(50),
@PhoneNumber int,
@EmailId1 varchar(50),
@CreatedDate date = NULL,
@CreatedById int = NULL
AS
SET NOCOUNT ON
INSERT INTO [dbo].[Cha]
([ChaName] ,[PortId],[StateId],[CountryId],[Address],[PhoneNumber],[EmailId1],[CreatedDate],[CreatedById],[IsActive])
VALUES(@ChaName, @PortId, @StateId, @CountryId,@Address,@PhoneNumber,@EmailId1,GETDATE(),@CreatedById,'true')
GO
.cs代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
namespace User_Login_CS.Inventory
{
public partial class Cha : System.Web.UI.Page
{
private string message = string.Empty;
SqlConnection conn;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
PopulatePortName();
PopulateStateName();
PopulateCountryName();
}
}
private Boolean InsertCha()
{
int intPortId = Convert.ToInt32(ddlPort.SelectedValue);
int intStateId = Convert.ToInt32(ddlState.SelectedValue);
int intCountryId = Convert.ToInt32(ddlCountry.SelectedValue);
Boolean blnSuccess = true;
try
{
conn = GetConnection();
SqlCommand cmd = new SqlCommand("Cha_Insert", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@ChaName", SqlDbType.VarChar).Value = txtChaName.Text;
cmd.Parameters.Add("@PortId", SqlDbType.Int).Value = ddlPort.SelectedValue;
cmd.Parameters.Add("@StateId", SqlDbType.Int).Value = ddlState.SelectedValue;
cmd.Parameters.Add("@CountryId", SqlDbType.Int).Value = ddlCountry.SelectedValue;
cmd.Parameters.Add("@Address", SqlDbType.VarChar).Value = txtAddress.Text;
cmd.Parameters.Add("@PhoneNumber", SqlDbType.Int).Value = txtPhone.Text;
cmd.Parameters.Add("@EmailId1", SqlDbType.VarChar).Value = txtEmail1.Text;
cmd.Parameters.Add("@CreatedById", SqlDbType.Int).Value = Convert.ToInt32(Session["userid"]);
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
blnSuccess = false;
lblmessage.Visible = true;
lblmessage.Text = "Error Occured: " + ex.Message;
}
return blnSuccess;
}
private void PopulatePortName()
{
conn = GetConnection();
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM [Port]", conn);
DataSet dsPort = new DataSet();
da.Fill(dsPort, "dsPort");
ddlPort.DataSource = dsPort.Tables["dsPort"];
ddlPort.DataTextField = "PortCode";
ddlPort.DataValueField = "PortId";
ddlPort.DataBind();
conn.Close();
}
private void PopulateStateName()
{
conn = GetConnection();
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM [PortState]", conn);
DataSet dsState = new DataSet();
da.Fill(dsState, "dsState");
ddlState.DataSource = dsState.Tables["dsState"];
ddlState.DataTextField = "StateName";
ddlState.DataValueField = "StateId";
ddlState.DataBind();
conn.Close();
}
private void PopulateCountryName()
{
conn = GetConnection();
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM [PortCountry]", conn);
DataSet dsCountry = new DataSet();
da.Fill(dsCountry, "dsCountry");
ddlCountry.DataSource = dsCountry.Tables["dsCountry"];
ddlCountry.DataTextField = "CountryName";
ddlCountry.DataValueField = "CountryId";
ddlCountry.DataBind();
conn.Close();
}
private Boolean CheckEmailId()
{
conn = GetConnection();
Boolean blnExists = false;
SqlDataAdapter ei = new SqlDataAdapter("SELECT * FROM Cha where EmailId1='" + txtEmail1.Text + "'", conn);
DataSet dsEmailId = new DataSet();
ei.Fill(dsEmailId);
if (dsEmailId.Tables[0].Rows.Count > 0)
{
blnExists = true;
//conn.Close();
}
else
{
blnExists = false;
}
return blnExists;
}
private SqlConnection GetConnection()
{
conn = new SqlConnection();
conn.ConnectionString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
conn.Open();
return conn;
}
protected void btnInsertCha_Click(object sender, EventArgs e)
{
//int intPortId = Convert.ToInt32(ddlPort.SelectedValue);
//int intStateId = Convert.ToInt32(ddlState.SelectedValue);
//int intCountryId = Convert.ToInt32(ddlCountry.SelectedValue);
Boolean blnExists = false;
blnExists = CheckEmailId();
Boolean blnSuccess = true;
if (!blnExists)
{
blnSuccess = InsertCha();
if (blnSuccess)
{
lblmessage.Visible = true;
lblmessage.Text = "User Successfully Cha Agent Registered!!!";
}
}
else
{
lblmessage.Visible = true;
lblmessage.Text = "Email Id Already Exists!!!";
}
}
}
}
表格结构
/****** Object: Table [dbo].[Cha] Script Date: 03/23/2016 12:26:47 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Cha](
[ChaId] [int] IDENTITY(1,1) NOT NULL,
[ChaName] [varchar](50) NOT NULL,
[PortId] [int] NOT NULL,
[StateId] [int] NULL,
[CountryId] [int] NOT NULL,
[Address] [varchar](50) NOT NULL,
[PhoneNumber] [int] NOT NULL,
[EmailId1] [varchar](50) NOT NULL,
[CreatedDate] [date] NOT NULL,
[CreatedById] [int] NULL,
[ModifiedDate] [date] NULL,
[ModifiedById] [int] NULL,
[IsActive] [bit] NOT NULL,
[ChaReferenceId] [varchar](50) NULL,
CONSTRAINT [PK_Cha_1] PRIMARY KEY CLUSTERED
(
[ChaId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
答案 0 :(得分:3)
您可以在创建表格时使用Computed列来执行此操作
CREATE TABLE [dbo].[Cha](
[ChaId] [int] IDENTITY(1,1) NOT NULL,
[ChaName] [varchar](50) NOT NULL,
[PortId] [int] NOT NULL,
[StateId] [int] NULL,
[CountryId] [int] NOT NULL,
[Address] [varchar](50) NOT NULL,
[PhoneNumber] [int] NOT NULL,
[EmailId1] [varchar](50) NOT NULL,
[CreatedDate] [date] NOT NULL,
[CreatedById] [int] NULL,
[ModifiedDate] [date] NULL,
[ModifiedById] [int] NULL,
[IsActive] [bit] NOT NULL,
[ChaReferenceId] AS ('YourTableName' + REPLACE(RIGHT(CONVERT(char(8), CreatedDate, 5), 5), '-', '') + RIGHT('00'+ CAST(ChaId as varchar), 3)),
CONSTRAINT [PK_Cha_1] PRIMARY KEY CLUSTERED
(
[ChaId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
对于计算列的数据类型
[ChaReferenceId] AS Convert(VARCHAR(50),('YourTableName' + REPLACE(RIGHT(CONVERT(char(8), CreatedDate, 5), 5), '-', '') + RIGHT('00'+ CAST(ChaId as varchar), 3)))