设为首页 加入收藏

TOP

C#VS2010连接数据库大全(五)
2014-11-24 03:31:05 来源: 作者: 【 】 浏览:4
Tags:C#VS2010 连接 数据库 大全
c DataSet Query(string sqlString, DataSet dataSet, string tableName)
{
using (System.Data.IDbConnection iConn = this.GetConnection())
{
using (System.Data.IDbCommand iCmd = GetCommand(sqlString, iConn))
{
iConn.Open();
try
{
System.Data.IDataAdapter iAdapter = this.GetAdapater(sqlString, iConn);
((OleDbDataAdapter)iAdapter).Fill(dataSet, tableName);
return dataSet;
}
catch (System.Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
if (iConn.State != ConnectionState.Closed)
{
iConn.Close();
}
}
}
}
}


/**/
///


/// 执行SQL语句 返回存储过程
///

/// Sql语句
/// 要填充的DataSet
/// 开始记录
/// 页面记录大小
/// 表名称
/// DataSet
public DataSet Query(string sqlString, DataSet dataSet, int startIndex, int pageSize, string tableName)
{
using (System.Data.IDbConnection iConn = this.GetConnection())
{
iConn.Open();
try
{
System.Data.IDataAdapter iAdapter = this.GetAdapater(sqlString, iConn);

((OleDbDataAdapter)iAdapter).Fill(dataSet, startIndex, pageSize, tableName);

return dataSet;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
if (iConn.State != ConnectionState.Closed)
{
iConn.Close();
}
}
}
}


/**/
///


/// 执行查询语句,向XML文件写入数据
///

/// 查询语句
/// XML文件路径
public void WriteToXml(string sqlString, string xmlPath)
{
Query(sqlString).WriteXml(xmlPath);
}

/**/
///


/// 执行查询语句
///

/// 查询语句
/// DataTable
public DataTable ExecuteQuery(string sqlString)
{
using (System.Data.IDbConnection iConn = this.GetConnection())
{
//System.Data.IDbCommand iCmd = GetCommand(sqlString,iConn);
DataSet ds = new DataSet();
try
{
System.Data.IDataAdapter iAdapter = this.GetAdapater(sqlString, iConn);
iAdapter.Fill(ds);
}
catch (System.Exception e)
{
throw new Exception(e.Message);
}
finally
{
if (iConn.State != ConnectionState.Closed)
{
iConn.Close();
}
}
return ds.Tables[0];
}
}

/**/
///


/// 执行查询语句
///

/// 查询语句
/// DataTable
public DataTable ExecuteQuery(string SqlString, string Proc)
{
using (System.Data.IDbConnection iConn = this.GetConnection())
{
using (System.Data.IDbCommand iCmd = GetCommand(SqlString, iConn))
{
iCmd.CommandType = CommandType.StoredProcedure;
DataSet ds = new DataSet();
try
{
System.Data.IDataAdapter iDataAdapter = this.GetAdapater(SqlString, iConn);
iDataAdapter.Fill(ds);
}
catch (System.Exception e)
{
throw new Exception(e.Message);
}
finally
{
if (iConn.State != ConnectionState.Closed)
{
iConn.Close();
}
}
return ds.Tables[0];
}


}
}

/**/
///


///
///

///
///
public DataView ExeceuteDataView(string Sql)
{
using (System.Data.IDbConnection iConn = this.GetConnection())
{
using (System.Data.IDbCommand iCmd = GetCommand(Sql, iConn))
{
DataSet ds = new DataSet();
try
{
System.Data.IDataAdapter iDataAdapter = this.GetAdapater(Sql, iConn);
iDataAdapter.Fill(ds);
return ds.Tables[0].DefaultView;
}
catch (System.Exception e)
{
throw new Exception(e.Message);
}
finally
{
if (iConn.State != ConnectionState.Closed)
{
iConn.Close();
}
}
}
}
}

#endregion

#region 执行带参数的SQL语句
/**/
///


/// 执行SQL语句,返回影响的记录数
///

/// SQL语句
/// 影响的记录数
public int ExecuteSql(string SQLString, params IDataParameter[] iParms)
{
using (System.Data.IDbConnection iConn = this.GetConnection())
{
System.Data.IDbCommand iCmd = GetCommand();
{
try
{
PrepareCommand(out iCmd, iConn, null, SQLString, iParms);
int rows = iCmd.ExecuteNonQuery();
iCmd.Parameters.Clear();
return rows;
}
catch (System.Exception E)
{
throw new Exception(E.Message);
}
finally
{
iCmd.Dispose();
if (iConn.State != C
首页 上一页 2 3 4 5 6 7 下一页 尾页 5/7/7
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇jsp连接数据库大全 下一篇Vertica用于时间计算的SQL语句大全

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·数据库:推荐几款 Re (2025-12-25 12:17:11)
·如何最简单、通俗地 (2025-12-25 12:17:09)
·什么是Redis?为什么 (2025-12-25 12:17:06)
·对于一个想入坑Linux (2025-12-25 11:49:07)
·Linux 怎么读? (2025-12-25 11:49:04)