传入一个地址于excel表名,返回一个datatable,里面是与userinf表里不相符的信息

晚上终于把一个困扰很久的验证实现了,但大师姐明天就走了,再加上瑞星免费期已到,一点都高兴不起来。

验证的类中代码:

 public bool validateUser(string uName, string uRealName)
        
{

            SqlParameter[] parameters 
= {
                    
new SqlParameter("@username", SqlDbType.NVarChar,256),
                    
new SqlParameter("@realname", SqlDbType.VarChar,250)
                    }
;
            parameters[
0].Value = uName;
            parameters[
1].Value = uRealName;

            
if (sdh.ExecuteSql4Value("select count(*) from UserInfo where UserName=@username and URealName=@realname", parameters) > 0)
            
{
                
return true;
            }

            
else
            
{
                
return false;
            }

        }




        
public DataTable validateUserErr(string path)
        
{
            eh.ExcelDB(path);
            OleDbDataReader dr 
= eh.execSQL4DR("SELECT * FROM [Sheet1$]");
            DataTable dt 
= new DataTable();
            DataRow row 
= dt.NewRow();
            
while (dr.Read())
            
{
                
string uid = dr["人员编号"].ToString();
                
string uname = dr["姓名"].ToString();

                
//Response.Write("人员姓名:" + uname + "   人员编号: " + uid + "-----");



                
if (validateUser(uid, uname))
                
{
                  
                    
//Response.Write("<font color=green>通过!</font>");

                }

                
else
                
{
                    dt.Columns.Add(
"人员编号");
                    dt.Columns.Add(
"姓名"); 
                    row[
"人员编号"= uid;
                    row[
"姓名"= uname;
                    dt.Rows.Add(row);
                    
//Response.Write("<font color=red>没这人!</font>");

                }


               
// Response.Write("<br />");
            }

            
return dt;
        }



ExcelDBHelper.cs中的代码

        public void ExcelDB(string ExcelPath)
        
{
            _ExcelPath 
= ExcelPath;
        }

 
public OleDbDataReader execSQL4DR(string sql)
        
{
            OleDbConnection conn 
= createEconn();
            OleDbCommand cmd 
= new OleDbCommand(sql, conn);
            
try
            
{
                conn.Open();
                
return cmd.ExecuteReader(CommandBehavior.CloseConnection);
            }

            
catch (Exception error)
            
{
                
throw error;
            }

            
finally
            
{
                
            }

        }





SqlDbHelper.cs类中的方法:

  /// <summary>
        
/// 执行SQL语句,返回第一行,第一列(sea)
        
/// </summary>
        
/// <param name="SQLString">sql语句</param>
        
/// <param name="cmdParms">参数数组</param>
        
/// <returns>int</returns>

        public int ExecuteSql4Value(string SQLString, params SqlParameter[] cmdParms)
        
{
            
using (SqlConnection connection = new SqlConnection(strConn))
            
{
                
using (SqlCommand cmd = new SqlCommand())
                
{
                    
try
                    
{
                        PrepareCommand(cmd, connection, 
null, SQLString, cmdParms);
                        
object r = cmd.ExecuteScalar();
                        
if (Object.Equals(r, null))
                        
{
                            
throw new Exception("value unavailable!");
                        }

                        
else
                        
{
                            
try int a = (int)r; }
                            
catch return 0; }
                            
return (int)r;
                        }


                    }

                    
catch (System.Data.SqlClient.SqlException E)
                    
{
                        
throw new Exception(E.Message);
                    }

                    
finally
                    
{
                        cmd.Parameters.Clear();
                    }

                }

            }

        }
posted on 2008-03-14 22:05  超少  阅读(255)  评论(1编辑  收藏  举报