none
System.Data.DataSet - insert into RRS feed

  • Question

  • I am beginner in .Net and this is my first quertion. If this is wrong forum for .Net data access, please correct me.

    I have a table with PK column of type int which is NOT identity column (I can not change it). I have to insert into table values from .Net using System.Data.DataSet. Is it possible or not? I have found some code:

    this.columnMy.AutoIncrement = true;
    this.columnMy.AutoIncrementSeed = -1;
    this.columnMy.AutoIncrementStep = -1;
    this.columnMy.AllowDBNull = false;
    this.columnMy.ReadOnly = true;
    this.columnMy.Unique = true;
    this.columnMy.AllowDBNull = false;

    is purpose of this code to generate autoincrement values if column is not identity column in DB table? Can I use it with this code:

    this._adapter.InsertCommand.Connection = this.Connection;
                this._adapter.InsertCommand.CommandText = "INSERT INTO [dbo].[MyTable] (ID_Column, ...)\r\nVALUES " +
                    "    (@ID_Column, ...); \r\nSELECT ID_Column... FROM MyTable WHERE (ID_Column = SCOPE_IDENTITY())";
                this._adapter.InsertCommand.CommandType = global::System.Data.CommandType.Text;

    • Moved by Amanda Zhu Tuesday, October 29, 2013 10:00 AM
    Monday, October 28, 2013 8:24 PM

Answers

  • Hello,

    As far as I know, there are several ways to generate the unique key.

    1.Using DateTime and HashCode

    Using DateTime for generating unique keys is very common practice. We can use this approach by inducing HashCode in it also. Like

    DateTime.Now.ToString().GetHashCode().ToString("x");

    2.Using DateTime and HashCode

    Guid.NewGuid().ToString().GetHashCode().ToString("x");

    3.UsingRNGCryptoServiceProvider and Character Masking

    The .net Framwork provides RNGCryptoServiceProvider class which Implements a cryptographic Random Number Generator (RNG) using the implementation provided by the cryptographic service provider (CSP). This class is usually used to generate random numbersRegards.

    private string GetUniqueKey()
    
      {
    
      int maxSize  = 8 ;
    
      int minSize = 5 ;
    
      char[] chars = new char[62];
    
      string a;
    
      a = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
    
      chars = a.ToCharArray();
    
      int size  = maxSize ;
    
      byte[] data = new byte[1];
    
      RNGCryptoServiceProvider  crypto = new RNGCryptoServiceProvider();
    
      crypto.GetNonZeroBytes(data) ;
    
      size =  maxSize ;
    
      data = new byte[size];
    
      crypto.GetNonZeroBytes(data);
    
      StringBuilder result = new StringBuilder(size) ;
    
      foreach(byte b in data )
    
      { result.Append(chars[__b % (chars.Length - )>); }
    
       <span>return result.ToString();
    
      }
    

    And for detail information, see this.

    If this does not work for you, please let me know.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, October 30, 2013 1:50 AM
    Moderator

All replies

  • Hi,

    I have moved this thread to ADO.NET DataSet forum for better response.

    Thanks,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, October 29, 2013 10:01 AM
  • I have a table with PK column of type int which is NOT identity column (I can not change it). I have to insert into table values from .Net using System.Data.DataSet. Is it possible or not?

    So, the real question is: can we in .Net assure unique values are generated and written into database primary key int column which is not defined as IDENTITY column? Does .Net have some built in code for this?

    Tuesday, October 29, 2013 10:08 AM
  • Hello,

    As far as I know, there are several ways to generate the unique key.

    1.Using DateTime and HashCode

    Using DateTime for generating unique keys is very common practice. We can use this approach by inducing HashCode in it also. Like

    DateTime.Now.ToString().GetHashCode().ToString("x");

    2.Using DateTime and HashCode

    Guid.NewGuid().ToString().GetHashCode().ToString("x");

    3.UsingRNGCryptoServiceProvider and Character Masking

    The .net Framwork provides RNGCryptoServiceProvider class which Implements a cryptographic Random Number Generator (RNG) using the implementation provided by the cryptographic service provider (CSP). This class is usually used to generate random numbersRegards.

    private string GetUniqueKey()
    
      {
    
      int maxSize  = 8 ;
    
      int minSize = 5 ;
    
      char[] chars = new char[62];
    
      string a;
    
      a = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
    
      chars = a.ToCharArray();
    
      int size  = maxSize ;
    
      byte[] data = new byte[1];
    
      RNGCryptoServiceProvider  crypto = new RNGCryptoServiceProvider();
    
      crypto.GetNonZeroBytes(data) ;
    
      size =  maxSize ;
    
      data = new byte[size];
    
      crypto.GetNonZeroBytes(data);
    
      StringBuilder result = new StringBuilder(size) ;
    
      foreach(byte b in data )
    
      { result.Append(chars[__b % (chars.Length - )>); }
    
       <span>return result.ToString();
    
      }
    

    And for detail information, see this.

    If this does not work for you, please let me know.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, October 30, 2013 1:50 AM
    Moderator