i am doing a project on C# in window form
i currently trying to do a button which can randomly create student but , those student should tag with a student id which should be auto generate..but i not sure how should i do the auto generate...i did something in the below code, but found that the student id is not in sequence...i need it to be in sequence is there a way to create a auto generate student id in sequence and tag with the random student data?? is student that is random and student id is sequence...the sequence i mean is it 1, 2,3....and i already have a primary key
thanks in advance
i create a class for the random data
public static class StudentRegister
{
private static Random s = new Random();
static string[] student = new string[] { "wei ting", "jin ling", "wei wei", "shi ya", "yi ting", "an qin", "lian en"};
public static string GetS开发者_JAVA百科tudent()
{
return student[s.Next(0, student.Count())];
}
static string[] studentID = new string[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20"" };
public static string GetStudentID()
{
return studentID[s.Next(0, studentID.Count())];
}
}
i try something like this: edited: but got a problem after i stop it will and re-run my form again, it call back 1 again, how am i going to change the code? can tell me...i want to it to continue like example previous i have already insert, 3 random name so the student id should 1,2,3 right, then i close this form and re-run again the form, and insert another 3 random name it start from 1 again not 4
public static class StudentRegister
{
static int NextID = 0;
private static Random s = new Random();
static string[] student = new string[] { "wei ting", "jin ling", "wei wei", "shi ya",
"yi ting", "an qin", "lian en" };
public static string GetStudent()
{
return student[s.Next(0, student.Count())];
}
public static string GetStudentID()
{
++NextID;
return NextID.ToString();
}
}
I would not handle auto-incrementing a unique id in your c# code. Let MSSQL do the work. Then you can insert new records (containing StudentName and whatever else you want) and the ID will be generated for you:
http://www.w3schools.com/Sql/sql_autoincrement.asp
Edit:
Okay, this is untested but something like this... I'd change the C# code:
public static class StudentRegister
{
private static Random s = new Random();
static string[] student = new string[] { "wei ting", "jin ling", "wei wei", "shi ya", "yi ting", "an qin", "lian en" };
public static string GetStudent()
{
return student[s.Next(0, student.Count())];
}
}
private void btnStudentAdd_Click(object sender, EventArgs e)
{
using (var conn = new SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=student;Integrated Security=True"))
{
conn.Open();
using (var cmd = new SqlCommand("INSERT INTO StudentList (StudentName) VALUES (@Name)", conn))
{
cmd.Parameters.Add("@Name", SqlDbType.VarChar);
for (int i = 0; i < 3; i++)
{
cmd.Parameters["@Name"].Value = StudentRegister.GetStudent();
cmd.ExecuteNonQuery();
}
}
conn.Close();
}
GridviewBind(); //bind the create data back to datagridview
}
You'll need to add the identity column to your table. I'm assuming there's no data in the table you need to keep, so just drop and recreate the table.
DROP TABLE StudentList;
CREATE TABLE StudentList
(
id int PRIMARY KEY,
StudentName varchar (15),
StudentId int IDENTITY(1,1)
);
精彩评论