SQL Server Fetch SQL data table source to display datagridview column (column created by using edit column)

ftf50wuq  于 2023-10-15  发布在  其他
关注(0)|答案(1)|浏览(123)

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"; 
    }
}

Question: I wish to display SQL source data to the datagridview column where I created by using edit column. Unfortunately, it won't works. Something missing out there. Expert help required.

vhmi4jdf

vhmi4jdf1#

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))
     {

     
         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();
             DataColumn column = new DataColumn();
             DataRow dataRow = dataTable.NewRow();

             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())
             {
                 int column1Value = reader.GetInt32(0); // Assuming it's an Boolean column
                 //column.DefaultValue = 0;
                 //Convert.ToInt32(column1Value);
                 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);
             }

相关问题