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

来源:互联网 发布:linux禅道数据库 编辑:程序博客网 时间:2024/05/22 03:03

 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();
        } 

原创粉丝点击