Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
Node is saved as draft in My Content >> Draft
  • Table Schema from SqlDataReader

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 75
    Comment on it

    While using ADO.NET classes we perform manipulations with data and tables.

     

    But if in any case we first want to know the table structure or want to get the schema structure then we can also use SqlDataReader class classes for that.

     

    This is to instantiate the SqlDataReader class :

     

    SqlDataReader sqlReader = sqlCmd.ExecuteReader();

     

     

    If you want to retrieve the schema information of table using GetSchemaTable method of SqlDataReader class you can retrieve it when the SqlDataReader is open.

     

     The below code example will return a DataTable Object, which contains the rows and columns information for the current result set.

    string connetionString = null;
                SqlConnection sqlCnn ;
                SqlCommand sqlCmd ;
                string sql = null;
    
                connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password";
                sql = "Select * from product";
    
                sqlCnn = new SqlConnection(connetionString);
                try
                {
                    sqlCnn.Open();
                    sqlCmd = new SqlCommand(sql, sqlCnn);
                    SqlDataReader sqlReader = sqlCmd.ExecuteReader();
                    DataTable schemaTable = sqlReader.GetSchemaTable();
    
                    foreach (DataRow row in schemaTable.Rows)
                    {
                        foreach (DataColumn column in schemaTable.Columns)
                        {
                            MessageBox.Show (string.Format("{0} = {1}", column.ColumnName, row[column]));
                        }
                    }
                    sqlReader.Close();
                    sqlCmd.Dispose();
                    sqlCnn.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Can not open connection ! ");
                }
            }
    .net

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: