我对 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)
{
}
}
}