我从excel中导入了些数据到数据库中,但是不知道怎么提示数据有重复。
只限一列的数据。
我写了个程序进行判断,但是总是不能实现,请大神帮帮忙!
                    
SqlCommand comm = conn.CreateCommand();                    
comm.CommandText = "select ass_catid from assets";     //获取数据库内catid的内容  
string d=comm.CommandText ;                 
com.Parameters["@固定资产类别ID"].Value = dt.Rows[i][j];          j++;
  
 //对固定资产编号的判断        
if (c != 11)     
{        
    MessageBox.Show("数据长度错误,请仔细检查第" + r + "行" + 2 + "列!");            
    conn.Close();       
}   
else       
{            
    if (dt.Rows [i][j].ToString ().Contains(d))         
   {   
       MessageBox.Show("与数据库数据有重复,请仔细检查第" + r + "行" + 2 + "列!");      
       conn.Close();              
   }
                  
   else      
   {           
      com.Parameters["@固定资产编号"].Value = dt.Rows[i][j];           
   }                
}
c#excel数据库

解决方案 »

  1.   

    DB数据查出来放在 DataTable 里,然后用 
    DataRow[] existsRow = DataTable.Select(string.Format("列='{0}'"));
    if (existsRow != null && existsRow.Length > 0)
    {
       // 存在
    }
      

  2.   

    contains(d)就是包含于呀,我用d来存储从数据库里面查找到的ass_catid的值,然后判断现在要导入的数据是不是包含于d,但是我就是不知道是不是写错了还是怎么回事,就是不能判断。很抱歉,你给的代码我看不懂,太混乱了。
      

  3.   

    string d=comm.CommandText ;  
    d是你的sql语句啊。你这判断包含sql语句吗?
      

  4.   

    string d=comm.CommandText ;  
    d不是你想要的数据正确读取数据方式之一为:
    con.Open();
    OleDbCommand command = new OleDbCommand(SqlString, con);
    OleDbDataReader reader = command.ExecuteReader();
    while (reader.Read())
    {
       string data = reader.GetString(0);
    }
    这个data才是你想要的数据值。你得再看看ado.net!