通过EXCEL导入数据到grideView的,再绑定dataset,就差写最后一段代码,把dataset的数据更新到数据库不会写,请问如何继续写下去?高手帮忙解决一下~~~不胜感激~~

解决方案 »

  1.   

    如果你的数据库存在,并且这个数据表也存在的话,你这么做试一下. Dim con As New SqlConnection(SourceConnection)
            Dim cmm As New SqlCommand("select * from 表名", con)
            Dim adp As New SqlDataAdapter()
            adp.SelectCommand = cmm
            Dim cb As New SqlCommandBuilder(adp)
            Try
                con.Open()            adp.Update(dataset.tables(0))
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
      

  2.   


    这是从dataGridView1中读的,你可以根据条件来读。 for (iRows = 0; iRows < intCount; iRows++)
                        {
                            progressBar1.Value = iRows;
                                sw[0] = "'" + dataGridView1.Rows[iRows].Cells[0].Value.ToString() + "'";
                                sw[1] = "'" + dataGridView1.Rows[iRows].Cells[1].Value.ToString() + "'";
                                sw[2] = "'" + dataGridView1.Rows[iRows].Cells[2].Value.ToString() + "'";
                                sw[3] = "'" + dataGridView1.Rows[iRows].Cells[3].Value.ToString() + "'";
                                sw[4] = "'" + dataGridView1.Rows[iRows].Cells[4].Value.ToString() + "'";
                                sw[5] = "'" + dataGridView1.Rows[iRows].Cells[5].Value.ToString() + "'";
                                sw[6] = "'" + dataGridView1.Rows[iRows].Cells[6].Value.ToString() + "'";
                                sw[7] = "'" + dataGridView1.Rows[iRows].Cells[7].Value.ToString() + "'";
                                sw[8] = "'" + dataGridView1.Rows[iRows].Cells[8].Value.ToString() + "'";
                                sw[9] = "'" + dataGridView1.Rows[iRows].Cells[9].Value.ToString() + "'";
      

  3.   


    SqlConnection con=new SqlConnection(SourceConnection);
    SqlCommand cmm=New SqlCommand("select 字段1,字段2...字段N from 表名", con);
    SqlDataAdapter adp=new SqlDataAdapter();
    adp.SelectCommand = cmm;
    SqlCommandBuilder cb=new SqlCommandBuilder(adp);try{
    con.Open();
    adp.Update(dtb);
    }
    //以下代码略.
      

  4.   

    有我帖过的代码http://topic.csdn.net/u/20080122/14/26c0aaf7-c282-4725-bc6d-ec99a2cf446d.htmlhttp://topic.csdn.net/u/20080225/14/805797cb-673c-4b18-b107-b906a14f4bc6.html
      

  5.   

    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Data.OleDb;
    using System.Data.SqlClient;
    public partial class _Default : System.Web.UI.Page 
    {
        string connStr = ConfigurationManager.ConnectionStrings["schoolConnectionString"].ConnectionString;
       
        
        protected void Page_Load(object sender, EventArgs e)
        
        {
        }     
        private DataSet CreateDataSource()
        {
            string strCon;
            strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("" + DropDownList1.Text + "" + TextBox1.Text + "") + "; Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'";
            OleDbConnection olecon = new OleDbConnection(strCon);        OleDbDataAdapter myda = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", strCon);        DataSet myds = new DataSet();
            myda.Fill(myds);        return myds;
        
          
        
         
        
        protected void Button1_Click(object sender, EventArgs e)
        {
            GridView1.DataSource = CreateDataSource();
            GridView1.DataBind();
        }
    }我原程序是这样的,从EXCEL导入到GridView,再绑定DATASET,但是后面的写入SQL,就不会了。
      

  6.   

    贴一段代码给你吧,你看一下里面的循环                foreach (DataTable table in myds.Tables)
                        foreach (DataRow row in table.Rows)
                        {
                            sqlstr = "insert into T_info (xm,xb,bz) values('" + row[0].ToString() + "','" + row[1].ToString() + "','" + row[2].ToString() + "')";
                            mycomm = new OleDbCommand(sqlstr, mydbconn);
                            mycomm.ExecuteNonQuery();
                        }这里是循环到 row ,
    你想要列的话,foreach(DataColumn column in table.Columns){}
      

  7.   

    protected void Button1_Click(object sender, EventArgs e) 
        { 
            string connectstring = "...";
            string sqlstr=string.Empty;
            DataSet myds = CreateDataSource(); 
            GridView1.DataSource = myds; 
            GridView1.DataBind();        
            
             SqlConnection myConn =new SqlConnection(connectstring);
            myConn.open();
            SqlCommand  myComm;
            foreach (DataTable table in myds.Tables)
              foreach (DataRow row in table.Rows)
              {
                sqlstr = "insert into T_info (xm,xb,bz) values('" + row[0].ToString() + "','" + row[1].ToString() + "','" + row[2].ToString() + "')";
                //这里改成你自己表的格式
                            mycomm = new OleDbCommand(sqlstr, mydbconn);
                            mycomm.ExecuteNonQuery();
                        }
            myConn.close();
        
        } 
      

  8.   

    //sqlstr 改成你自己表的格式
      

  9.   


                    //这里改成你自己表的格式 
                    mycomm = new OleDbCommand(sqlstr, mydbconn);
                    mycomm.ExecuteNonQuery();
               
    错误 1 当前上下文中不存在名称“mycomm”
    错误 2 当前上下文中不存在名称“mydbconn”
    错误 3 当前上下文中不存在名称“mycomm”
    请问怎样解决呢??
      

  10.   

    protected void Button1_Click(object sender, EventArgs e)  
        {  
            string connectstring = "..."; 
            string sqlstr=string.Empty; 
            DataSet myds = CreateDataSource();  
            GridView1.DataSource = myds;  
            GridView1.DataBind();         
             
             SqlConnection myConn =new SqlConnection(connectstring); 
            myConn.open(); 
            SqlCommand  myComm; 
            foreach (DataTable table in myds.Tables) 
              foreach (DataRow row in table.Rows) 
              { 
                sqlstr = "insert into T_info (xm,xb,bz) values('" + row[0].ToString() + "','" + row[1].ToString() + "','" + row[2].ToString() + "')"; 
                //这里改成你自己表的格式 
                            myComm = new SqlCommand(sqlstr, myConn); 
                            myComm.ExecuteNonQuery(); 
                        } 
            myConn.close(); 
         
        }