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;
}
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;
}
解决方案 »
- 求助~!!!
- 菜鸟急求,数组添加
- 各位,麻烦看看怎么写这个
- 请教下大家一个编码问题。
- 蛙蛙推荐:微软网络讲座系列教程视频下载(2004年1月到2005年4越)
- 急急急急!vs自动生成的TableAdapter有没有Connection属性呢
- :请问如何读出以下XML文件里的记录信息,解决了马上加分~
- dropdownlist下拉框里的文字和页面上的其它文字看上去不一样,不知道怎么解决?
- 有图片就显示,没有就不显示,我是通过Repeater来显示数据的!高手请进!icyer()你在吗?
- 小白求问 如何在.asp文件中调用别的文件里的函数返回值?
- 急!高分求救!WEB中调用WORD打印报表的问题.解决完就结贴!
- 在浏览器Show窗体时,出来下面的安全提示怎么去掉?(解决就给分)
''' 检查输入的用户名是否已经存在
'''</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
if(IsExist("select 1 from 用户表 where 用户名='新用户名'"))
Response.Write("用户名已存在");
else
Response.Write("用户名可以注册");
if(ObjDt.Rows[0][0] == 1)
// 唯一
else
// 不唯一
注册的时候先把别人提交用户名字去select一下,看结果办楼!
--------------------------------------
上海京拓科技(www.kingtoo.com)火热8月促销
100M asp.net空间,支持1.1, 2.0 只要 60元/年
国际顶级域名 50元/年
50M Sql2000数据库 200元/年
代理享受更低价格,请访问www.kingtoo.com
如果是sqlserver之类的数据库的话可以使用存储过程来处理,比较快速