积极答复者
怎么判断DataTable中,某一列的值是否重复了?

问题
-
我没有用到数据库,当我把数据添加到dataset中时,DataTable设了主键;在插入DataRow时,怎么来判断主键列的值已经存在了?
大其心,可容天下之物; 虚其心,可受天下之善;- 已移动 Sheng Jiang 蒋晟Moderator 2010年1月23日 22:38 System.Data (发件人:Visual C#)
答案
-
- 已标记为答案 lfofiug 2010年1月19日 3:22
-
修改连接字符串
我从excel 中读数据,可是读到的和excel中真实的有点差别:比如excel中的数据为: L990990,我用c#程序导入excel,读到的是:990990。这是为什么啊
大其心,可容天下之物; 虚其心,可受天下之善;Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\MyExcel.xls;Extended Properties="Excel 8.0;HDR=Yes;IMEX=1";http://www.connectionstrings.com/excel不同问题请最好分贴发
http://feiyun0112.cnblogs.com/- 已标记为答案 lfofiug 2010年1月19日 3:22
-
你是否是去掉表中某一列重复的记录啊?如果是这样采用下面的方式就可以了。
DataTable source = new DataTable(); source.Columns.Add("Id", typeof(String)); source.Columns.Add("Name", typeof(String)); source.Rows.Add(new object[] { "01-0001", "Sam" }); source.Rows.Add(new object[] { "01-0001", "Sam" }); source.Rows.Add(new object[] { "01-0002", "Judy" }); DataTable target = source.Clone(); target.PrimaryKey = new DataColumn[] { target.Columns["Id"] }; foreach (DataRow sourceRow in source.Rows) { if (target.Select(String.Format("Id = '{0}'", sourceRow["Id"])).Length == 0) target.ImportRow(sourceRow); }
知识改变命运,奋斗成就人生!- 已标记为答案 lfofiug 2010年1月19日 3:22
全部回复
-
- 已标记为答案 lfofiug 2010年1月19日 3:22
-
修改连接字符串
我从excel 中读数据,可是读到的和excel中真实的有点差别:比如excel中的数据为: L990990,我用c#程序导入excel,读到的是:990990。这是为什么啊
大其心,可容天下之物; 虚其心,可受天下之善;Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\MyExcel.xls;Extended Properties="Excel 8.0;HDR=Yes;IMEX=1";http://www.connectionstrings.com/excel不同问题请最好分贴发
http://feiyun0112.cnblogs.com/- 已标记为答案 lfofiug 2010年1月19日 3:22
-
你可以用datatable .select判断
datatable dt;
string str="id='ff3'";//模糊查询 //string str=" id like 's%'";
DataRow[] drws=dt.Select(str);
if(drws.Length>0)
{
}
努力+方法=成功
当我执行:DataRow[] pp = dt.Select("VIP=" + dr["VIP"]); 就会出错了;其中VIP是列名,dr["VIP"]为列值:L909090;
提示我:未找到列“L909090”
这是为什么啊?
大其心,可容天下之物; 虚其心,可受天下之善; -
你是否是去掉表中某一列重复的记录啊?如果是这样采用下面的方式就可以了。
DataTable source = new DataTable(); source.Columns.Add("Id", typeof(String)); source.Columns.Add("Name", typeof(String)); source.Rows.Add(new object[] { "01-0001", "Sam" }); source.Rows.Add(new object[] { "01-0001", "Sam" }); source.Rows.Add(new object[] { "01-0002", "Judy" }); DataTable target = source.Clone(); target.PrimaryKey = new DataColumn[] { target.Columns["Id"] }; foreach (DataRow sourceRow in source.Rows) { if (target.Select(String.Format("Id = '{0}'", sourceRow["Id"])).Length == 0) target.ImportRow(sourceRow); }
知识改变命运,奋斗成就人生!- 已标记为答案 lfofiug 2010年1月19日 3:22