如何列出所有数据库';在winforms中的datagridview中的表,并在每个表上附加一个复选框,以对所选表
本文关键字:复选框 一个 winforms 中的 datagridview 何列出 数据库 | 更新日期: 2023-09-27 18:28:19
我对C#非常陌生,正在尝试开发一种工具来重新索引sql server数据库的选定表,这样用户就不必进入数据库并运行任何命令。
namespace DBTool
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
public DataSet GetTableNames()
{
try
{
//Local variables
DataSet resultSet = new DataSet();
//Get the connection string from the config file
string connectionString = ConfigurationManager.ConnectionStrings["DBTool.Properties.Settings.ConStr"].ConnectionString;
//Create a new database connection
SqlConnection connection = new SqlConnection(connectionString);
//Set the stored procedure 'sproc_Get_Tables_Names' as the command to be executed
using (SqlCommand cmd = new SqlCommand("sproc_Get_Tables_Names", connection))
{
//Setup the command object
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandTimeout = 0;
//Open the connection
if (connection.State != ConnectionState.Open)
{
connection.Open();
}
//Execute the query and fill out the dataset
adapter.Fill(resultSet);
//Close the connection
connection.Close();
//Return the result of the stored procedure
return resultSet;
}
}
catch (Exception ex)
{
MessageBox.Show("An error occurred due to the following exception: " + ex.ToString() + ".","Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
return null;
}
}
private void button1_Click(object sender, EventArgs e)
{
DataSet set = GetTableNames();
dataGridView1.DataSource = set.Tables[0];
}
private void dataGridView1_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
{
}
}
}
我针对我的一个数据库(使用不同的存储过程)运行了您的代码,它可以工作。您是否收到任何异常?请尝试将连接字符串的超时设置为较低值,然后重新运行。
代码工作的事实意味着SQL Server/Network Connectivity/Permissions是一个问题,并且超时时间太长,您永远不会得到异常。
一些尝试:
-
存储的进程在服务器上执行正常吗?
-
如果是,它返回了多少数据?你回来了吗数据量,而且需要很长时间?
-
连接字符串提供的用户是否有权限执行存储的proc?
-
可以使用SQL管理连接到SQL Server吗工作室使用相同的凭据?