i want to genrete a auto number in my project....
i used vb .net 2008 and SQl server 2005 as backend ??
i want to create a serial no that is like abc/2010/01..In this..
- the abc is same in all the serial no.
- the 2010 is used from the running Year.(using date for year)
- the 01 is a actual serial no that can be auto genrete...
But how can i do this ....?? and How can i find max number from my serial no.....??? how can i maintain it if i delete it then all after delete serial no will place it's place..(there is no break in serial n开发者_如何学JAVAo on delete) ?????? Please help me.....
here is the code snippet for generate newid i have taken a simple label and textbox label will auto generate newid and textbox will insert a new record in database take a label and textbox in your design page
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
public static int temp;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
newcode();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=practice;Integrated Security=True;");
con.Open();
SqlCommand cmd = new SqlCommand("Insert into frmlogin(id,username) values ("+ temp+",'"+TextBox1.Text+"')", con);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("username", TextBox1.Text);
cmd.ExecuteNonQuery();
con.Close();
TextBox1.Text = "";
Label1.Text = "";
newcode();
}
public void newcode()
{
SqlConnection con1 = new SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=practice;Integrated Security=True;");
con1.Open();
SqlCommand cmd2 = new SqlCommand("Select Max(id) as empid from frmlogin", con1);
cmd2.CommandType = CommandType.Text;
SqlDataReader r = cmd2.ExecuteReader();
r.Read();
if (r["empid"].ToString() != "")
{
temp = int.Parse(r["empid"].ToString()) + 1;
}
else
{
temp = 1;
}
Label1.Text= temp.ToString();
r.Close();
con1.Close();
}
}
The main thing is that you should maintain your incremented number somewhere, In my case i have used textbox and label.
But I did rather recommend you to go for GUID or Random Number.
for GUID :
System.Guid.NewGuid().ToString();
Have Look here as well
for Random Number Generation
::: Program that uses Random type [C#] :::
using System;
class Program
{
static void Main()
{
Random random = new Random();
Console.WriteLine(random.Next());
Console.WriteLine(random.Next());
}
}
::: Output of the program :::
1592498984
1526415661
Try this
You could store the serial numbers in a database table where the third component of the number is the primary key of the table as an identity field. Any time you create a new serial number, you'd insert the current year and the static text into the table. An easy way to do this would be to wrap that logic in a stored procedure which takes no arguments, internally inserts the current year and the text, and returns the entire serial number as a single scalar value.
Something like:
INSERT INTO SerialNumbers (Year, Name) VALUES (year(getdate()), 'abc')
and:
SELECT Name + '/' + CAST(Year AS nvarchar) + '/' + CAST(ID AS nvarchar) FROM SerialNumbers WHERE ID = SCOPE_IDENTITY()
精彩评论