private void button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(@"Data Source=.;AttachDbFilename=C:\Users\Amit\Documents\ghf.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
SqlDataAdapter sda = new SqlDataAdapter(("create table #xyz(pid int, pname nvarchar(50),pamount nvarchar(50),cid int,cname nvarchar(50)"), con);
dt = new DataTable();
sda.Fill(dt);
dataGridView1.DataSource = dt;
}
SqlConnection con = new SqlConnection(@"Data Source=.;AttachDbFilename=C:\Users\Amit\Documents\ghf.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
SqlCommand cmd = new SqlCommand("insert into #xyz (pid, pname, pcost , cid ,cname) select product.pid,product.pname,product.pcost ,category.cid ,category.cname from product inner join category on product.cid = category.cid", con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
dataGridView1.DataSource = dt;
答案 0 :(得分:0)
临时表仅对创建它的连接可见。
要使用临时表,您的插入SqlCommand
必须使用与创建表con
相同的SqlCommand
对象,而不会在其间关闭连接。
要按原样使用代码,必须使表成为实际表而不是临时表(除去#),但是您的button1_click
事件处理程序需要先检查表是否存在在您尝试创建它之前,之前运行的数据仍然在其中。