CSDN博客

img stlwj

SQL-SERVER的读写临时表

发表于2004/10/18 15:16:00  845人阅读

   DataTable myda=new DataTable();

   DataColumn dcName=myda.Columns.Add("f2");
   dcName.DataType=Type.GetType("System.String");
   dcName=myda.Columns.Add("f7");
   dcName.DataType=Type.GetType("System.String");
   dcName=myda.Columns.Add("f9");
   dcName.DataType=Type.GetType("System.String");

   DataRow dr;
   for (int i=0;i<ds.Tables[0].Rows.Count;i++)
   {
    dr=myda.NewRow();    
    dr[0]=ds.Tables[0].Rows[i]["f2"].ToString().Remove(ds.Tables[0].Rows[i]["f2"].ToString().Length-1,1);
    dr[1]=ds.Tables[0].Rows[i]["f7"].ToString();
    dr[2]=ds.Tables[0].Rows[i]["f9"].ToString();
    myda.Rows.Add(dr);
   }
   this.MySqlConn=new SqlConnection("data source=192.168.0.11;initial catalog=olderp;persist security info=False;user id=sa;pwd=zyd0501;workstation id=LJY;packet size=4096");
   this.MySqlConn.Open();
   myCm=new SqlCommand();
   myCm.Connection=MySqlConn;
   for(int i=0;i<myda.Rows.Count-1;i++)
   {
    myCm.CommandText="if object_id('tempdb.dbo.#abc') is null begin create table tempdb.#abc (f2 varchar(14),f7 numeric(10,2),f9 varchar(14)) end; insert into tempdb.#abc (f2,f7,f9) values ('"+myda.Rows[i][0].ToString()+"','"+Convert.ToDouble(myda.Rows[i][1])+"','"+myda.Rows[i][2].ToString()+"')";
    myCm.ExecuteNonQuery();
   }
   //myCm.CommandText="select * from tempdb.#abc";
   MySqlAdapter=new SqlDataAdapter();

   MySqlAdapter.SelectCommand=myCm;
   this.MySqlAdapter.SelectCommand.CommandText="select * from tempdb.#abc,lwj_view where tempdb.#abc.f2=lwj_view.productid and lwj_view.priceid='RMJ'";

   //MySqlAdapter.SelectCommand=;
   DataSet ds2=new DataSet();
   this.MySqlAdapter.Fill(ds2);
   this.label1.Text="新表内容";
   this.MyGrid.DataSource=ds2.Tables[0];
   this.saveFileDialog1.Filter="xml files (*.xml)|*.xml";
   this.saveFileDialog1.FilterIndex=1;
   if(saveFileDialog1.ShowDialog() == DialogResult.OK)
   {
    if(this.saveFileDialog1.FileName!="")
     ds2.WriteXml(this.saveFileDialog1.FileName);
    MessageBox.Show("导出数据成功!");

阅读全文
0 0

相关文章推荐

img
取 消
img