private string   m_dbConnection=null;
protected string   strSQL=null; public Database()
{
//
// TODO: Add constructor logic here
//
m_dbConnection=System.Configuration.ConfigurationSettings.AppSettings["ConnectionSqlServer"];
if (m_dbConnection==null) throw new Exception("Error-> DSN not set in Config.web");
} //PUBLIC:Return a DataReader ------------------
public SqlDataReader GetDataReader(string selectCmd)
{
SqlConnection m_SqlConnection = new SqlConnection(m_dbConnection);
SqlCommand m_SqlCommand = new SqlCommand(selectCmd,m_SqlConnection);
try
{
m_SqlCommand.Connection.Open();
}
catch(Exception e)
{
throw new Exception("Error in CInfo:GetDataReader()-> " + e.ToString());
}
return m_SqlCommand.ExecuteReader(CommandBehavior.CloseConnection);
} //PUBLIC: Return DataSet : See dbSelect -------------------------
public DataSet GetDataSet(string selectCmd,string Table0) // Select From Table
{
DataSet ds = null;
SqlConnection m_SqlConnection = new SqlConnection(m_dbConnection);
SqlDataAdapter m_SqlDataAdapter = new SqlDataAdapter(selectCmd,m_SqlConnection);
try 
{
ds = new DataSet();
m_SqlDataAdapter.Fill(ds,Table0);
} // end try
catch (Exception e)
{
throw new Exception("Error in CInfo:GetDataSet()-> " + e.ToString());
}
finally

m_SqlDataAdapter.Dispose();
m_SqlConnection.Close();
m_SqlConnection.Dispose();
}
return ds; 
} // end GetDataSet
public bool IsExist(string sql) 
{
bool IsTrue=false;

SqlConnection m_SqlConnection = new SqlConnection(m_dbConnection);
SqlCommand    m_SqlCommand    = new SqlCommand(sql, m_SqlConnection);
try 
{
m_SqlCommand.Connection.Open();
SqlDataReader dr= m_SqlCommand.ExecuteReader();
if (dr.Read()) 
{
IsTrue=true;
}
} // end try
catch (Exception e)
{
throw new Exception("Error in IsExist:GetDataReader()-> " + e.ToString());
} finally


m_SqlConnection.Close();
m_SqlConnection.Dispose();
}
return IsTrue;

}  //PUBLIC: Non-Query Sql Function --------------------------------
public bool SqlFunction(string sqlStr)
{
SqlConnection m_SqlConnection = new SqlConnection(m_dbConnection);
SqlCommand    m_SqlCommand    = new SqlCommand(sqlStr, m_SqlConnection);
try 
{
m_SqlConnection.Open();
m_SqlCommand.ExecuteNonQuery();
}
catch (Exception e)
{
// return false; 
throw new Exception("Error in CInfo:SqlFunction()-> " + e.ToString());
}
finally

m_SqlCommand.Dispose();
m_SqlConnection.Close();
m_SqlConnection.Dispose();
}
return true; 
} // End SqlFunction 
public DataSet GetPageDataSet(int PageSize,int CurrentPageIndex,string selectCmd,string Table0)
{
DataSet ds = null;
SqlConnection m_SqlConnection = new SqlConnection(m_dbConnection);
SqlDataAdapter m_SqlDataAdapter = new SqlDataAdapter(selectCmd,m_SqlConnection);

try 
{
ds = new DataSet();
m_SqlDataAdapter.Fill(ds,PageSize*(CurrentPageIndex-1),PageSize,Table0);
} // end try
catch (Exception e)
{
throw new Exception("Error in CNews:GetDataSet()-> " + e.ToString());
}
finally

m_SqlDataAdapter.Dispose();
m_SqlConnection.Close();
m_SqlConnection.Dispose();
}
return ds; 
} public int RecordCount(string countCmd) // Get Count of Records
{
int recCount=0;  // Return Value 
SqlConnection m_SqlConnection = new SqlConnection(m_dbConnection);
SqlCommand    m_SqlCommand    = new SqlCommand(countCmd,m_SqlConnection);
try 
{
// Open the Connection ----------------------------
m_SqlCommand.Connection.Open();
recCount = (int)m_SqlCommand.ExecuteScalar();
} // end try
catch (Exception e)
{
throw new Exception("Error in CInfo:RecordCount()-> " + e.ToString());
//recCount=0;
}
finally

m_SqlCommand.Dispose();
m_SqlConnection.Close();
m_SqlConnection.Dispose();
}
return recCount; 
} // end RecordCount public string GetField(string sql) 
{
string str_field="";

SqlConnection m_SqlConnection = new SqlConnection(m_dbConnection);
SqlCommand    m_SqlCommand    = new SqlCommand(sql, m_SqlConnection);
try 
{
m_SqlCommand.Connection.Open();
SqlDataReader dr= m_SqlCommand.ExecuteReader();
if (dr.Read()) 
{
str_field=dr[0].ToString();
}
} // end try
catch (Exception e)
{
throw new Exception("Error in IsExist:GetField()-> " + e.ToString());
} finally


m_SqlConnection.Close();
m_SqlConnection.Dispose();
}
return str_field;

}

解决方案 »

  1.   

    通过数据库查,reader返回有值的话,就不能注册咯
      

  2.   

    太笼统了...........不明白ing......
      

  3.   

    '''<summary>
        ''' 检查输入的用户名是否已经存在    
        '''</summary>
        '''<returns>
        ''' 如果已经存在 返回 True
        ''' 如果不存在 返回 False
        '''</returns>
        Public Shared Function isUserNameExist(ByVal UserName As String) As Boolean
            Dim dbConnection As System.Data.IDbConnection = New System.Data.OleDb.OleDbConnection(ConnectionString)
            Dim queryString As String = "SELECT [ID] FROM [Users] WHERE Name = @Name"
            Dim dbCommand As OleDbCommand = New OleDbCommand
            dbCommand.CommandText = queryString
            dbCommand.Connection = dbConnection        dbCommand.Parameters.Add("@Name", OleDbType.VarChar, 25).Value = UserName
            dbConnection.Open()
            Dim dataReader As System.Data.IDataReader = dbCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
            Dim isExist As Boolean = False
            If dataReader.Read Then
                isExist = True
            Else
                isExist = False
            End If
            Return isExist
        End Function
      

  4.   

    你上面不是写好了吗?直接调用就好了。
    if(IsExist("select 1 from 用户表 where 用户名='新用户名'"))
     Response.Write("用户名已存在");
    else
     Response.Write("用户名可以注册");
      

  5.   

    select count(*) from TableName where UserName = 'XXX'DataTable ObjDt = ....
    if(ObjDt.Rows[0][0] == 1)
      // 唯一
    else
      // 不唯一
      

  6.   

    对,用户名不能为一,表里面肯定有个字段是用户名字
    注册的时候先把别人提交用户名字去select一下,看结果办楼!
      

  7.   

    学习
    --------------------------------------
    上海京拓科技(www.kingtoo.com)火热8月促销
    100M asp.net空间,支持1.1, 2.0 只要 60元/年
    国际顶级域名 50元/年
    50M Sql2000数据库 200元/年
    代理享受更低价格,请访问www.kingtoo.com
      

  8.   

    注册的时候先执行一个查询,看用户名是否存在,存在就不给注册。
    如果是sqlserver之类的数据库的话可以使用存储过程来处理,比较快速