Resources » Code Snippets » ADO.NET

CommandBehavior


Posted Date: 10-Dec-2010  Last Updated:   Category: ADO.NET    
Author: Member Level: Gold    Points: 30


In this article, i would like to share information about command's keyinfo. In general we use the command object for performing operations such as Insert, update, delete and select. But we can use the command's keyinfo to get the "Column Name" of the table too.



KeyInfo


----------------

I would like to share some more useful information about the command available in .net 2.0. The command object supports various methods like executenonquery, executescalar for the purpose of insert ,update, delete or select operation. How ever it also has the ability to understand the table's column name too.

Using the keyinfo property it is possible to find out the column name of the table. Let us walk thru to know about it.


CommandBehavior.KeyInfo


-----------------------
This property is very useful one in getting the name of the field as well the datatype of them.

Example


-------

The following example would enable us to get the columnname of the table and its datatype associated with.



protected void btnShow_Click(object sender, EventArgs e)
{
try
{
string strConn = "Data Source=SSEd166\\sql2008;user id=sa;password=$sse123;initial catalog=master";
SqlConnection cn = new SqlConnection(strConn);
cn.Open();

SqlCommand cmd = new SqlCommand("select * from ticketdetails", cn);

SqlDataReader rd = cmd.ExecuteReader(CommandBehavior.KeyInfo);


string Column1DataType = rd.GetFieldType(0).ToString();
string Column2DataType = rd.GetFieldType(1).ToString();
string Column3DataType = rd.GetFieldType(2).ToString();
string Column1Name = rd.GetName(0).ToString();
string Column2Name = rd.GetName(1).ToString();
string Column3Name = rd.GetName(2).ToString();

rd.Close();

Response.Write("Field 1 Data Type --->" + Column1DataType + "<br>");
Response.Write("Field 2 Data Type --->" + Column2DataType + "<br>");
Response.Write("Field 3 Data Type --->" + Column3DataType + "<br>");


Response.Write("Field 1 Name" + Column1Name + "<br>");
Response.Write("Field 2 Name" + Column2Name + "<br>");
Response.Write("Field 3 Name" + Column3Name + "<br>");



while (rd.Read())
{
Response.Write(rd[0].ToString() + rd[1].ToString());
}
}
catch (Exception ex)
{
Response.Write(ex.Message.ToString());
}
}




Executing the above code will give the name of the table columns as well the datatype of it.

This may be very useful in many situations.


Did you like this resource? Share it with your friends and show your love!

Responses to "CommandBehavior"

No responses found. Be the first to respond...

Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Submit Article     Return to Article Index

    Subscribe to Subscribers
    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Talk to Webmaster Tony John

    Online Members

    Pawan Awasthi
    More...
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India