Thursday, March 31, 2011

any way to detect a TimeStamp column in a DataTable from SQL Server (fillSchema???)

When updating a datatable from a "select * from sometable", I am iterating the columns in the datatable to dynamically create an update or insert statement, based on datatable.rowstate and DataTable.PrimaryKey.

However, I can't seem to find any property that indicates whether a column is a TimeStamp in SQL Server and must therefore be ignored in the update statement!!!

From stackoverflow
  • I would say that you would need to know the row types ahead of time. Unless you wrote an SQL script that did a describe on the table you are copying from and looked for a timestamp format.

    tbone : I'm trying to figure out a way to do it, not a confirmation that you don't know how to do it.
  • Try using the GetSchemaTable method of the SqlDataReader class. It will return metadata for each column. The specific properties you'll be interested in are DataTypeName and isRowVersion.

    string sql = "SELECT * FROM SomeTable";
    using ( SqlConnection cn = new SqlConnection ())
    {
       cn.ConnectionString = "Your Connection String";
       cn.Open ();
       using ( SqlCommand cmd = new SqlCommand ( sql, cn ) )
       {
          SqlDataReader dataReader = cmd.ExecuteReader ( CommandBehavior.KeyInfo );
          dataReader.Read ();
    
          DataTable dt = dataReader.GetSchemaTable ();
          foreach ( DataRow r in dt.Rows )
          {
            Console.WriteLine ( "Column name: {0}\t Data Type: {1}\t IsRowVersion:{2}", r["ColumnName"].ToString (), r["DataTypeName"].ToString (), r["isRowVersion"].ToString() );
          }
    
        }
     }
    

0 comments:

Post a Comment