db-多表分组统计
作者:互联网
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace FindTwoTables
{
public partial class Frm_Main : Form
{
public Frm_Main()
{
InitializeComponent();
}
private void Frm_Main_Load(object sender, EventArgs e)
{
dgv_Message.DataSource = GetMessage();//设置数据源
}
private void btn_Select_Click(object sender, EventArgs e)
{
dgv_Message.DataSource = GetBook();//设置数据源
}
/// <summary>
/// 查询数据库信息
/// </summary>
/// <returns>方法返回DataTable对象</returns>
private DataTable GetBook()
{
string P_Str_ConnectionStr = string.Format(//创建数据库连接字符串
@"server=.;database=db_TomeTwo;uid=sa;pwd=Server2012");
string P_Str_SqlStr = string.Format(//创建SQL查询字符串
@"SELECT k.书号,k.书名,x.作者, SUM(k.现存数量) AS
现存数量 ,SUM(x.销售数量) AS 销售数量 FROM tb_Vendition AS x ,
tb_BookMessage AS k where x.书号=k.书号 GROUP BY k.书号,
k.书名,x.作者, k.现存数量 ORDER BY 书号");
SqlDataAdapter P_SqlDataAdapter = new SqlDataAdapter(//创建数据适配器
P_Str_SqlStr, P_Str_ConnectionStr);
DataTable P_dt = new DataTable();//创建数据表
P_SqlDataAdapter.Fill(P_dt);//填充数据表
return P_dt;//返回数据表
}
/// <summary>
/// 查询数据库信息
/// </summary>
/// <returns>方法返回DataTable对象</returns>
private DataTable GetMessage()
{
string P_Str_ConnectionStr = string.Format(//创建数据库连接字符串
@"server=.;database=db_TomeTwo;uid=sa;pwd=Server2012");
string P_Str_SqlStr = string.Format(//创建SQL查询字符串
@"SELECT * FROM tb_BookMessage,tb_Vendition WHERE
tb_BookMessage.书号=tb_Vendition.书号");
SqlDataAdapter P_SqlDataAdapter = new SqlDataAdapter(//创建数据适配器
P_Str_SqlStr, P_Str_ConnectionStr);
DataTable P_dt = new DataTable();//创建数据表
P_SqlDataAdapter.Fill(P_dt);//填充数据表
return P_dt;//返回数据表
}
}
}
标签:多表,string,SqlDataAdapter,db,System,分组,Str,using,DataTable 来源: https://blog.csdn.net/dxm809/article/details/96718900