Want to build the ChatGPT based Apps? Start here
Become a member
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
C# Corner Home
Technologies
Monthly Leaders
ASK A QUESTION
Forum guidelines
Abhijit Parab
2.2k
11
2.5k
How to obtain primary key of table
Feb 5 2013 3:50 AM
Hi,
I want to know column which has primary key defined in sql server database.
Following is my code
SqlConnection con = new SqlConnection("Initial Catalog=Dbname; Data Source=MySqlServer; User=testuser; Password=pass123;");
DataSet ds = new DataSet();
SqlCommand cmd = new SqlCommand("select * from TableName", con);
cmd.CommandType = CommandType.Text;
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
DataColumn[] columns;
columns = ds.Tables[0].PrimaryKey;
// Get the number of elements in the array.
Response.Write("Column Count: " + columns.Length);
for (int i = 0; i < columns.Length; i++)
{
Response.Write(columns[i].ColumnName + columns[i].DataType);
}
In fact, i have defined one primary key on a column of TableName
But my code returns zero count.
please correct me, if i am wrong and provide me alternative solution
thank is advance.
Reply
Answers (
1
)
Change smtp from mail address using c#
Displaying Sum of two values in Gridview wiout saing in Data