如何在数据库中自动编号?

时间:2012-09-17 20:46:03

标签: c# sql-server windows winforms sql-server-2008

我使用C#Windows窗体。我需要自动编号不重复。

我将文件excel导入到datagridview并生成字段'id'而不是重复。

但是不要工作。

实施例

首先导入。 ID |名称 P001 => Auto Gen |一个 P002 => Auto Gen |乙

第二次导入。 ID |名称 P001 => Auto Gen但我需要P003 | C P002 => Auto Gen但我需要P004 | d

代码:

    SqlConnection Conn = new SqlConnection();
        SqlCommand cmd = new SqlCommand();
        StringBuilder sb = new StringBuilder();
        SqlTransaction tr;

        private void testExcel_Load(object sender, EventArgs e)
        {
            string appConn = ConfigurationManager.ConnectionStrings["connDB"].ConnectionString;
            Conn = new SqlConnection();
            if (Conn.State == ConnectionState.Open)
            {
                Conn.Close();

            }
            Conn.ConnectionString = appConn;
            Conn.Open();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {

                if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    TextBox1.Text = openFileDialog1.FileName;
                    File.ReadAllText(TextBox1.Text);
                }
                OleDbConnection conn = new OleDbConnection();

                conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" + TextBox1.Text + @";Extended Properties=""Excel 8.0; HDR=Yes; IMEX=1; ImportMixedTypes=Text; TypeGuessRows=0"" ";
                OleDbCommand cmd = new OleDbCommand("SELECT * " + "FROM [SHEET1$]", conn);
                DataSet ds = new DataSet();
                OleDbDataAdapter ad = new OleDbDataAdapter(cmd);
                ad.Fill(ds);

                dataGridView1.DataSource = ds.Tables[0];
            }
            catch (Exception)
            {

            }
        }

private void button2_Click(object sender, EventArgs e)
        {
                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    tr = Conn.BeginTransaction();

                    sb.Remove(0, sb.Length);
                    sb.Append("INSERT INTO tbl_Asset (AsId,AsName)");
                    sb.Append("VALUES (@Asid,@AsName)");
                    string sqlSave = sb.ToString();

                    cmd.CommandText = sqlSave;
                    cmd.CommandType = CommandType.Text;
                    cmd.Connection = Conn;
                    cmd.Transaction = tr;
                    cmd.Parameters.Clear();

                    cmd.Parameters.Add("@Asid", SqlDbType.VarChar).Value = dataGridView1.Rows[i].Cells[0].Value;
                    cmd.Parameters.Add("@AsName", SqlDbType.VarChar).Value = dataGridView1.Rows[i].Cells[1].Value;

                    cmd.ExecuteNonQuery();
                    tr.Commit();

                }
                MessageBox.Show("Insert Done", "Result", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            int cellnum = 0;

            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                cellnum = cellnum + 1;
                dataGridView1.Rows[i].Cells[0].Value = txtID.Text + "000" + cellnum;                
            }
        }

非常感谢你的时间。 :)

1 个答案:

答案 0 :(得分:0)

如果你纯粹在SQL Server中这样做,你可以add an identity column然后添加computed column来为你提供自定义格式:

alter table MyTable add MyNewColumn as 'P' + right('000' + cast(MyIdentColumn as varchar(3)), 3)

请注意,此示例会在您的身份达到1000后给出意外结果。