如何在 C# 中基于表中的一列显示记录

本文关键字:一列 记录 显示 于表中 | 更新日期: 2023-09-27 18:32:47

>它显示错误:表没有主键

DataSet ds = new DataSet("ds1");
DataTable dt = ds.Tables.Add("tblcategory");
string s = textBox1.Text.ToString();


DataRow foundRow = ds.Tables["tblcategory"].Rows.Find(s);
if (foundRow != null)
{
    MessageBox.Show(foundRow[0].ToString());
}

如何在 C# 中基于表中的一列显示记录

您必须设置表结构,特别是主键列,以便能够找到带有 Find 的行,例如:

var column = dt.Columns.Add("Id", typeof(int));
column.AllowDBNull = false;
column.Unique = true;