Here we will combine two SQL queries within single command and using dataset we can store data separately into two DataTable
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;
namespace WindowsForm
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click_1(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=Nayab\\SQL_INSTANCE2;Initial Catalog=test;Integrated Security=True";
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select * from name;select * from friend";
cmd.Connection = con;
SqlDataAdapter ad = new System.Data.SqlClient.SqlDataAdapter();
ad.SelectCommand = cmd;
DataSet ds = new DataSet();
ad.Fill(ds);
this.dataGridView1.DataSource = ds.Tables[0];
this.dataGridView2.DataSource = ds.Tables[1];
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;
namespace WindowsForm
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click_1(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=Nayab\\SQL_INSTANCE2;Initial Catalog=test;Integrated Security=True";
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select * from name;select * from friend";
cmd.Connection = con;
SqlDataAdapter ad = new System.Data.SqlClient.SqlDataAdapter();
ad.SelectCommand = cmd;
DataSet ds = new DataSet();
ad.Fill(ds);
this.dataGridView1.DataSource = ds.Tables[0];
this.dataGridView2.DataSource = ds.Tables[1];
}
}
}
No comments:
Post a Comment