高分求!!!
最近我一直为gridview问题发愁, 我这有个小题目:
在页面中选择sqoserver服务器,输入用户名和密码登陆到相应的服务器上,显示所有的数据库,点击某个显示该数据库的所有表,对表进行增删改。
大家给点思路。。。
[解决办法]
这个很简单.用sql语句可以获得所有数据库名和相应数据库内表名,然后将他显示在页面上,点超连接显示更详细内容而已.超简单的题目
[解决办法]
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace ClassLib
{
public class OperationSQL
{
public OperationSQL()
{
}
/// <summary>
/// 获得所有数据库名称
/// </summary>
/// <param name="connectionString">连接字符串 </param>
/// <param name="isunion">是否新增‘--All Database--’行 </param>
/// <returns>所有数据库名称 </returns>
public static DataSet GetAllDataBase(string connectionString, bool isunion)
{
string strSql = "select [name] from sysdatabases union select '--All Database--' as name";
if (!isunion)
{
strSql = "select [name] from sysdatabases order by name";
}
DbHelperSQL.connectionString = connectionString;
return DbHelperSQL.Query(strSql);
}
/// <summary>
/// 获得指定数据库中的所有的表
/// </summary>
/// <param name="connection">连接 </param>
/// <returns>指定数据库中的所有的表 </returns>
public static DataSet GetAll(SqlConnection connection)
{
string strSql = "select [name] from sysObjects where xtype='V' or xtype='P' or xtype='U'and [name] <>'dtproperties' order by [name]";
return DbHelperSQL.Query(strSql, connection);
}
/// <summary>
/// 获得指定数据库中的所有的表
/// </summary>
/// <param name="connection">连接 </param>
/// <returns>指定数据库中的所有的表 </returns>
public static DataSet GetAllTable(SqlConnection connection)
{
string strSql = "select [name] from sysObjects where xtype='U'and [name] <>'dtproperties' order by [name]";
return DbHelperSQL.Query(strSql, connection);
}
/// <summary>
/// 获得指定数据库中的所有的视图
/// </summary>
/// <param name="connection">连接 </param>
/// <returns>指定数据库中的所有的视图 </returns>
public static DataSet GetAllView(SqlConnection connection)
{
string strSql = "select [name] from sysObjects where xtype='V' order by [name]";
return DbHelperSQL.Query(strSql, connection);
}
/// <summary>
/// 获得指定数据库中的所有的存储过程
/// </summary>
/// <param name="connection">连接 </param>
/// <returns>指定数据库中的所有的存储过程 </returns>
public static DataSet GetAllProc(SqlConnection connection)
{
string strSql = "select [name] from sysObjects where xtype='P' order by [name]";
return DbHelperSQL.Query(strSql, connection);
}
/// <summary>
/// 获得指定表中所有的列
/// </summary>
/// <param name="connection">连接 </param>
/// <param name="TableName"> </param>
/// <returns>指定表中所有的列 </returns>
public static DataSet GetAllTableClumn(SqlConnection connection, string TableName)
{
string strSql = "Select a.name as ColumnName,b.name as TypeName from syscolumns a, systypes b, sysObjects c where a.xtype = b.xusertype and a.id = c.id and c.name='" + TableName + "' order by a.colorder";
return DbHelperSQL.Query(strSql, connection);
}
/// <summary>
/// 获得指定表中所有的列
/// </summary>
/// <param name="connection">连接 </param>
/// <param name="TableName"> </param>
/// <returns>指定表中所有的列 </returns>
public static DataSet GetAllTableClumnProc(SqlConnection connection, string TableName)
{
string strSql = "Select a.name as ColumnName,a.isoutparam as isoutparam,b.name as TypeName from syscolumns a, systypes b, sysObjects c where a.xtype = b.xusertype and a.id = c.id and c.name='" + TableName + "' order by a.colorder";
return DbHelperSQL.Query(strSql, connection);
}
/// <summary>
/// 执行 use 查询。如:use master
/// </summary>
/// <param name="connection">连接 </param>
/// <param name="DbName">数据库名 </param>
/// <returns> </returns>
public static DataSet ExecuteQuery(SqlConnection connection, string DbName)
{
string strSql = "use " + DbName;
return DbHelperSQL.Query(strSql, connection);
}
/// <summary>
/// 查询表视图和存储过程
/// </summary>
/// <param name="connectionString"> </param>
/// <param name="database"> </param>
/// <param name="objectName"> </param>
/// <returns> </returns>
public static DataSet QueryObject(string connectionString, string database, string objectName)
{
string strSql = "use " + database + ";select a.name,c.name as datatype,a.isnullable,a.length,a.isoutparam from syscolumns a ";
strSql = (strSql + " join systypes c on a.xusertype=c.xusertype join sysobjects d on a.id=d.id ") + " where d.name='" + objectName + "'";
using (SqlConnection connection = new SqlConnection(connectionString))
{
DataSet ds = new DataSet();
try
{
connection.Open();
SqlDataAdapter command = new SqlDataAdapter(strSql, connection);
command.Fill(ds, "ds");
}
catch (System.Data.SqlClient.SqlException ex)
{
throw new Exception(ex.Message);
}
return ds;
}
}
}
}