Douglas wrote:
> Has anyone used the GetSchema method from
> System.Data.SqlClient.SqlConnection class?
> 
> I'd like to have a help on how to find out wich columns are
> Identity... 
> 
> If there is another way of finding that out, I would appreciate if
> someone could help as well...
> 
> thanks,
> 
> Douglas

Tia
                private void GeneraDataSet()
                {
                        SqlConnection cn = new SqlConnection(cnText);
                        SqlCommand cm = new SqlCommand("SELECT * FROM 
Customers",cn);
                        SqlDataReader dr;

                        try
                        {
                                cn.Open();
                                dr = cm.ExecuteReader(CommandBehavior.KeyInfo);
                                DataTable schemaTable = dr.GetSchemaTable();
                                if (schemaTable != null)
                                {
                                        ArrayList pkCols = new ArrayList();

                                        DataTable dataTable = new DataTable();
                                        dataTable.TableName = "menudef";

                                        foreach (DataRow schemaRow in 
schemaTable.Rows)
                                        {

                                                DataColumn col = new 
DataColumn();
                                                col.ColumnName = 
schemaRow["ColumnName"].ToString();
                                                col.DataType = 
(Type)schemaRow["DataType"];
                                                // set the length of the field 
for string types only
                                                if 
(schemaRow["DataType"].ToString() == "System.String")
                                                        col.MaxLength = 
(Int32)schemaRow["ColumnSize"];
                                                col.Unique = 
(bool)schemaRow["IsUnique"];
                                                col.AllowDBNull = 
(bool)schemaRow["AllowDBNull"];
                                                col.AutoIncrement = 
(bool)schemaRow["IsAutoIncrement"];
                                                
                                                // If part of the key, add the 
column name to the
                                                // array of columns comprising 
the primary key.

                                                if ((bool)schemaRow["IsKey"])
                                                                pkCols.Add(col);
                                                dataTable.Columns.Add(col);
                                        }

                                        // Add the primary key to the table.
                                        if (pkCols.Count > 0) 
                                          dataTable.PrimaryKey =
                                                
(DataColumn[])pkCols.ToArray(typeof(DataColumn));
                                        // Add the table to the DataSet.
                                        dsDati.Tables.Add(dataTable);

                                        object[] aData = new 
object[dataTable.Columns.Count];
                                        // Read all rows from the DataReader.
                                        while (dr.Read())
                                        {
                                                // Read the row from the 
DataReader into an array.
                                                dr.GetValues(aData);
                                                // Add the row from the array 
to the DataTable.
                                                dataTable.Rows.Add(aData);
                                        }
                                        dr.Close();
                                        dsDati.AcceptChanges();
                                }
                                
                        }
                        catch (Exception ex)
                        {
                                MessageBox.Show(ex.Message);
                                throw;
                        }
                        finally
                        {
                                if (cn.State == ConnectionState.Open)
                                        cn.Close();
                        }
                }

-------------------------------------------
Bastianello Luciano - MBS CP Development
Software Consultant - Apprentice Sorcerer
http://community.visual-basic.it/LucianoB/
e-mail: [EMAIL PROTECTED]
MSN: [EMAIL PROTECTED] - ICQ: 209754422
AIM: lubastia - YAHOO lbastianello
-------------------------------------------





------------------------ Yahoo! Groups Sponsor --------------------~--> 
Get Bzzzy! (real tools to help you find a job). Welcome to the Sweet Life.
http://us.click.yahoo.com/KIlPFB/vlQLAA/TtwFAA/XGgtlB/TM
--------------------------------------------------------------------~-> 

 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/Microsofts_C_Sharp/

<*> To unsubscribe from this group, send an email to:
    [EMAIL PROTECTED]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 


Reply via email to