日本好好热aⅴ|国产99视频精品免费观看|日本成人aV在线|久热香蕉国产在线

  • <cite id="ikgdy"><table id="ikgdy"></table></cite>
    1. 西西軟件園多重安全檢測(cè)下載網(wǎng)站、值得信賴(lài)的軟件下載站!
      軟件
      軟件
      文章
      搜索

      首頁(yè)西西教程數(shù)據(jù)庫(kù)教程 → 通過(guò)在Excel 2003中把數(shù)據(jù)添加的Sqlserver數(shù)據(jù)庫(kù)里怎么實(shí)現(xiàn)

      通過(guò)在Excel 2003中把數(shù)據(jù)添加的Sqlserver數(shù)據(jù)庫(kù)里怎么實(shí)現(xiàn)

      相關(guān)軟件相關(guān)文章發(fā)表評(píng)論 來(lái)源:本站整理時(shí)間:2010/12/17 10:47:32字體大小:A-A+

      作者:佚名點(diǎn)擊:360次評(píng)論:0次標(biāo)簽: Excel2003 Sqlserver winform datagrid

      • 類(lèi)型:編程輔助大。63KB語(yǔ)言:中文 評(píng)分:10.0
      • 標(biāo)簽:
      立即下載
      1、有沒(méi)有辦法對(duì)一個(gè)Excel進(jìn)行編程,讓他可以實(shí)現(xiàn)在模板里excel表格里新增數(shù)據(jù)到sqlserver中,也就是在excel表格里放個(gè)新增按鈕和保存按鈕,實(shí)現(xiàn)將填入的數(shù)據(jù)保存入數(shù)據(jù)庫(kù)中。

      2、或者將上述的excel表格嵌入在winform程序窗口里實(shí)現(xiàn)在EXcel表格里輸入數(shù)據(jù)及新增按鈕和保存按鈕來(lái)保存的功能。

      你干嘛不把excel編輯好了以后再導(dǎo)入數(shù)據(jù)庫(kù)呢?這方面比較容易實(shí)現(xiàn),要想在excel中編程和數(shù)據(jù)庫(kù)交互比較困難。

      winform中可以使用datagrid控件實(shí)現(xiàn)excel的編輯功能,這個(gè)和數(shù)據(jù)庫(kù)交互很容易實(shí)現(xiàn)。

      private void btnOpen_Click(object sender, EventArgs e)
      {
      OpenFileDialog ofd = new OpenFileDialog();

      ofd.Filter = "*.xls|";
      ofd.CheckFileExists = true;
      ofd.CheckPathExists = true;
      ofd.ShowDialog();
      this.textBox1.Text = ofd.FileName.ToString();

      }

      private void btnInsert_Click(object sender, EventArgs e)
      {
      DataSet ds = new DataSet();
      string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; " + "Data Source =" + this.textBox1.Text + ";Extended Properties=Excel 8.0";
      OleDbConnection myConn = new OleDbConnection(strCon);
      string sql = "Select * FROM [Sheet1$]";
      try
      {
      myConn.Open();
      OleDbDataAdapter oda = new OleDbDataAdapter(sql, myConn);

      oda.Fill(ds, "[Sheet1$]");
      myConn.Close();

      }
      catch (Exception ex)
      {

      MessageBox.Show(ex.Message);
      }


      string server = this.txtServer.Text.Trim();
      string db = this.txtDB.Text.Trim();
      string username = this.txtUserName.Text.Trim();
      string userpwd = this.txtUserPwd.Text.Trim();

      //這里的連接用來(lái)將數(shù)據(jù)寫(xiě)入SQLDB
      string connectionString = @"server="+server+"; database="+db+"; uid="+username+";pwd="+userpwd+"";
      SqlConnection con = new SqlConnection(connectionString);


      string sqlGetAllDB = "select * from tb_Lot";
      SqlDataAdapter daAllDB = new SqlDataAdapter(sqlGetAllDB, con);
      DataSet dsAllDB = new DataSet();


      DataGrid mygrid = new DataGrid();
      mygrid.BeginInit();
      mygrid.Location = new System.Drawing.Point(10, 240);
      mygrid.Width = 1020;
      mygrid.Height = 300;
      this.Controls.Add(mygrid);
      mygrid.EndInit();
      mygrid.SetDataBinding(ds, "[Sheet1$]");

      try
      {
      int num = ds.Tables[0].Rows.Count;
      for (int i = 0; i < num; i++)
      {
      string ch1 = mygrid[i, 0].ToString();
      string ch2 = mygrid[i, 1].ToString();
      string ch3 = mygrid[i, 2].ToString();
      string strii = "select * into from" + ds.Tables[0].TableName;
      string strsql = "insert into tb_Lot values('" + ch1 + "','" + ch2 + "','" + ch3 + "')";
      con.Open();
      System.Data.DataTable dt = new System.Data.DataTable("tb_Lot");
      SqlDataAdapter da = new SqlDataAdapter(strsql, con);
      da.Fill(dt);
      this.lblmessage.Text = "數(shù)據(jù)導(dǎo)入成功!";
      this.groupBox2.Visible = true;
      daAllDB.Fill(dsAllDB, "tb_Lot");
      this.dataGridView1.DataSource = dsAllDB.Tables[0];

      con.Close();
      }
      }
      catch (Exception ex)
      {

      MessageBox.Show("數(shù)據(jù)庫(kù)連接失!");
      }
      }

      還不如這樣直接將Excel的數(shù)據(jù)導(dǎo)入的數(shù)據(jù)庫(kù)表中,在Excel里面來(lái)操作的話難度比較高。這方法我自己用過(guò),可以將Excel的數(shù)據(jù)逐條導(dǎo)入的表中,前提是Excel的列數(shù)和表中的字段必須對(duì)應(yīng)。樓主可以研究下EXCEL里的VBA,順便再看看VBA里的ADODB.

        相關(guān)評(píng)論

        閱讀本文后您有什么感想? 已有人給出評(píng)價(jià)!

        • 8 喜歡喜歡
        • 3 頂
        • 1 難過(guò)難過(guò)
        • 5 囧
        • 3 圍觀圍觀
        • 2 無(wú)聊無(wú)聊

        熱門(mén)評(píng)論

        最新評(píng)論

        發(fā)表評(píng)論 查看所有評(píng)論(0)

        昵稱(chēng):
        表情: 高興 可 汗 我不要 害羞 好 下下下 送花 屎 親親
        字?jǐn)?shù): 0/500 (您的評(píng)論需要經(jīng)過(guò)審核才能顯示)