如何把datagrid中数据、dataset中数据导入SQL数据库

    技术2022-05-19  21

     1、 如何把datagrid中数据导入SQL数据库?                  if (this.DataGrid1.Items.Count > 0)            {                foreach (DataGridItem oDataGridItem in DataGrid1.Items)                {                     string strTheHour = oDataGridItem.Cells[0].Text.ToString();                     string sql = "Insert into   Temp(TheHour)  values(" + strTheHour + ")";                                        Connect.ExecuteSql(sql);                                    }            }      

     

    2、如何在dataset中数据插入SQL数据库         SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["SQLConnectionString"]);        try        {                              SqlDataAdapter myda = new SqlDataAdapter(sql, conn);            DataSet myds = new DataSet();            conn.Close();            conn.Open();            myda.Fill(myds, "" +strTable+ "");

                foreach (System.Data.DataRow row in myds.Tables[0].Rows)            {                string strTheHour = row["TheHour"].ToString().Trim();                string sql = "Insert into Temp(TheHour)  values(" + strTheHour + ")";                Connect.ExecuteSql(sql);            }            this.DataGrid1.DataSource = myds.Tables["" + strTable + ""].DefaultView;            this.DataGrid1.DataBind();            conn.Close();                        this.Response.Write(" <script language=javascript>alert('OK!');</script>");         }        catch (Exception exc)        {            conn.Close();                        string s = exc.ToString();        } 


    最新回复(0)