-1

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

我将文件 excel 导入到 datagridview 并为不重复生成字段“id”。

但不要工作。

前任。

首次导入。身份证 | 名称 P001 => 自动生成 | A P002 => 自动生成 | 乙

第二次进口。身份证 | 名称 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;                
            }
        }

非常感谢你的时间。:)

4

1 回答 1

0

如果您纯粹在 SQL Server 中执行此操作,则可以添加一个标识列,然后添加一个计算列以提供您的自定义格式:

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

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

于 2012-09-17T21:39:05.220 回答