【问题标题】:C# How to load data into DataGridView from multible tables [SQL Server]C# 如何将数据从多个表加载到 DataGridView [SQL Server]
【发布时间】:2016-11-20 21:14:26
【问题描述】:

如何将 2 个 SQL Server 表中的数据加载到 1 个 datagridview 中?就像我有 2 张桌子一样:

Jobs:Job_ID, Client_ID

EmployeeJobs:Emp_ID, Job_ID, Hours_Spent, Job_Date

我希望它们出现在一个datagridview中,正确的方法是什么?

【问题讨论】:

标签: c# sql database


【解决方案1】:

将 datagridview 绑定到以下 SQL 选择:

SELECT Jobs.Job_ID, Jobs.Client_ID, EmployeeJobs.Emp_ID, EmployeeJobs.Job_ID, EmployeeJobs.Hours_Spent, EmployeeJobs.Jobs_Date
FROM Jobs
INNER JOIN EmployeeJobs
ON Jobs.Job_ID=EmployeeJobs.Job_ID;

【讨论】:

    【解决方案2】:

    SQL JOIN 表,将结果绑定到网格。

    【讨论】:

      【解决方案3】:

      这会做你想做的。

      using System;
      using System.Data;
      using System.Data.SqlClient;
      using System.Windows.Forms;
      
      namespace WindowsApplication1
      {
          public partial class Form1 : Form
          {
              string connetionString;
              SqlConnection connection;
              SqlDataAdapter adapter;
              SqlCommandBuilder cmdBuilder;
              DataSet ds = new DataSet();
              DataSet changes;
              string Sql;
              Int32 i; 
      
              public Form1()
              {
                  InitializeComponent();
              }
      
              private void button1_Click(object sender, EventArgs e)
              {
                  connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password";
                  connection = new SqlConnection(connetionString);
                  Sql = "select * from Product";
                  try
                  {
                      connection.Open();
                      adapter = new SqlDataAdapter(Sql, connection);
                      adapter.Fill(ds);
                      connection.Close();
                      dataGridView1.DataSource = ds.Tables[0];
                  }
                  catch (Exception ex)
                  {
                      MessageBox.Show (ex.ToString());
                  }
              }
      
              private void button2_Click(object sender, EventArgs e)
              {
                  try
                  {
                      cmdBuilder = new SqlCommandBuilder(adapter);
                      changes = ds.GetChanges();
                      if (changes != null)
                      {
                          adapter.Update(changes);
                      }
                      MessageBox.Show("Changes Done");
                  }
                  catch (Exception ex)
                  {
                      MessageBox.Show(ex.ToString());
                  }
              }
          }
      }
      

      这是另一种方法。

      using System;
      using System.Data;
      using System.Windows.Forms;
      using System.Data.SqlClient;
      
      namespace WindowsApplication1
      {
          public partial class Form1 : Form
          {
              public Form1()
              {
                  InitializeComponent();
              }
      
              private void button1_Click(object sender, EventArgs e)
              {
                  string connectionString = "Data Source=.;Initial Catalog=pubs;Integrated Security=True";
                  string sql = "SELECT * FROM Authors";
                  SqlConnection connection = new SqlConnection(connectionString);
                  SqlDataAdapter dataadapter = new SqlDataAdapter(sql, connection);
                  DataSet ds = new DataSet();
                  connection.Open();
                  dataadapter.Fill(ds, "Authors_table");
                  connection.Close();
                  dataGridView1.DataSource = ds;
                  dataGridView1.DataMember = "Authors_table";
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-12-06
        • 2020-02-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-11-14
        相关资源
        最近更新 更多