获取SQL数据表源显示datagridview列(使用edit列创建的列)

问题描述 投票:0回答:1

private void button6_Click(object sender, EventArgs e)
{
    string serverName = textBox1.Text;
    string dbName = textBox4.Text;
    string username = textBox2.Text;
    string password = textBox3.Text;
    string tableName = textBox6.Text;

    string connectionString = $"Server={serverName};Database={dbName};User Id={username};Password={password};";

    using (SqlConnection fetchConnection = new SqlConnection(connectionString))
    {
        // connection.Open();
        // Check if the connection is open
        // if (connection == null || connection.State != ConnectionState.Open)
        // {
        //    MessageBox.Show("Please establish a database connection first.", "Connection Required", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        //    return;
        // }

        // Fetch data from the SQL data source
        SqlDataAdapter adapter = new SqlDataAdapter($"SELECT sc.name as [Column Name]\r\n\r\nFROM sys.all_columns sc\r\n\r\nJOIN sys.tables st\r\n\r\n ON st.object_id = sc.object_id\r\n\r\nWHERE st.name = '{tableName}' --and schema_id in ('16', '15')\r\n\r\nORDER BY st.name", fetchConnection); //Select YourTable

        DataTable dataTable = new DataTable();
        adapter.Fill(dataTable);

        // If you want to rename a column to "Column2"
        // dataTable.Columns["Column Name"].ColumnName = "Column2";

        // Bind the DataGridView to the retrieved data
        dataGridView1.DataSource = dataTable;
        // dataGridView2.DataSource = dataTable;

        // Replace with your column name
        dataGridView1.Columns["Column3"].DataPropertyName = "ColumnNameP"; 
    }
}

问题:我希望将SQL源数据显示到我使用编辑列创建的datagridview列。不幸的是,它不会起作用。那里缺少一些东西。需要专家帮助。

c# sql-server winforms datagridview datagridviewcolumn
1个回答
0
投票

private void button6_Click(对象发送者,EventArgs e) {

 string serverName = textBox1.Text;
 string dbName = textBox4.Text;
 string username = textBox2.Text;
 string password = textBox3.Text;
 string tableName = textBox6.Text;

 string connectionString = $"Server={serverName};Database={dbName};User Id={username};Password={password};";
 using (SqlConnection fetchConnection = new SqlConnection(connectionString))
 {


     //connection.Open();
     // Check if the connection is open
     //if (connection == null || connection.State != ConnectionState.Open)
     //{
     //MessageBox.Show("Please establish a database connection first.", "Connection Required", MessageBoxButtons.OK, MessageBoxIcon.Warning);
     //    return;
     //}

     // Fetch data from the SQL data source
     //SqlDataAdapter adapter = new SqlDataAdapter($"SELECT \r\n0 as [Select],\r\nst.name as 'Table Name',\r\nsc.name as 'Column Name',\r\n'' as 'New Value'\r\nFROM sys.all_columns sc JOIN sys.tables st ON st.object_id = sc.object_id \r\nWHERE st.name = 'bass_vessel' ", fetchConnection); //Select YourTable
     //DataTable dataTable = new DataTable();
     //adapter.Fill(dataTable);

     try
     {
         fetchConnection.Open();

         

         SqlCommand command = new SqlCommand($"SELECT \r\n0 as 'Select',\r\nst.name as 'Table Name',\r\nsc.name as 'Column Name',\r\n'' as 'New Value'\r\nFROM sys.all_columns sc JOIN sys.tables st ON st.object_id = sc.object_id \r\nWHERE st.name = 'bass_vessel' \r\n", fetchConnection);
         SqlDataReader reader = command.ExecuteReader();

         DataTable dataTable = new DataTable();
         dataTable.Columns.Add("Select", typeof(Boolean)); // Example: Add an Boolean column
         dataTable.Columns.Add("Table Name", typeof(string)); // Example: Add a string column
         dataTable.Columns.Add("Column Name", typeof(string)); // Add more columns as needed
         dataTable.Columns.Add("New Value", typeof(string)); // Add more columns as needed
        

         while (reader.Read())
         {
             Boolean column1Value = reader.GetBoolean(0); // Assuming it's an Boolean column
             string column2Value = reader.GetString(1); // Assuming it's a string column
             string column3Value = reader.GetString(2); // Assuming it's a string column
             string column4Value = reader.GetString(3); // Assuming it's a string column
             dataTable.Rows.Add(column1Value, column2Value, column3Value, column4Value);
         }

         reader.Close();

         dataGridView1.DataSource = dataTable;
     }
     catch (Exception ex)
     {
         Console.WriteLine("Error Occur: " + ex.Message);
     }
     finally
     {
         fetchConnection.Close();
   
     }
© www.soinside.com 2019 - 2024. All rights reserved.