程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> dataset-數據庫表的插入問題,數據集的更新

dataset-數據庫表的插入問題,數據集的更新

編輯:編程綜合問答
數據庫表的插入問題,數據集的更新

需求:假設SQL數據庫中有數據庫mydb,mydb中有一個表tb_repo,字段為時間,事件名稱,
事件描述,假設我想把同樣字段的一張excel表插入到tb_repo中,如何實現?

我在C#中實現方法:讀取excel表,並將它填充到數據集dataset1,然後再將tb_repo讀取
到dataset2,然後用Merge()函數合並,合並成功了,但我不知道如何將新的數據集寫入、
數據庫更新tb_repo。

請問我的實現是否有問題?(我只要求整張表插入的方法,最好不是一條記錄一條記錄的插入)

最佳回答:


我這裡剛好有個代碼,昨晚剛研究出來
實際效果就是將Dataset 內修改後的內容批量更新到數據庫內!

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace UseUpdate
{
    public partial class Frm_Main : Form
    {
        public Frm_Main()
        {
            InitializeComponent();
        }

        private DataSet G_st = new DataSet();//聲明表字段

        private void Frm_Main_Load(object sender, EventArgs e)
        {
            G_st.Tables.Add(new DataTable());
            GetMessage();//填充表
            dgv_Message.DataSource = G_st.Tables[0];//設置數據源
            dgv_Message.Columns[0].Visible = false;//隱藏主鍵列
        }

        private void btn_Submit_Click(object sender, EventArgs e)
        {
            SqlDataAdapter P_SqlDataAdapter = new SqlDataAdapter();//創建數據適配器
            SqlCommand P_cmd = new SqlCommand(@"UPDATE [dbo].[備品] SET MPN=@MPN,品牌=@品牌,結存數量=@結存數量,貨位號=@貨位號 WHERE ID=@ID", new SqlConnection(@"server=192.168.60.18;database=浙江先芯科技;uid=sa;pwd=lierda"));
            P_cmd.Parameters.Add("@ID", SqlDbType.Int, 10, "ID");//設置參數
            P_cmd.Parameters.Add("@MPN", SqlDbType.NVarChar, 10, "MPN");//設置參數
            P_cmd.Parameters.Add("@品牌", SqlDbType.NVarChar, 10, "品牌");//設置參數
            P_cmd.Parameters.Add("@結存數量", SqlDbType.Int, 10, "結存數量");//設置參數
            P_cmd.Parameters.Add("@貨位號", SqlDbType.NVarChar, 10, "貨位號");//設置參數
            P_SqlDataAdapter.UpdateCommand = P_cmd;//設置UpdateCommand屬性
            P_SqlDataAdapter.Update(G_st.Tables[0]);//更新數據庫中數據
            G_st.AcceptChanges();//提交修改
            MessageBox.Show("更改成功!","提示!");//彈出消息對話框
            GetMessage();//填充表
            dgv_Message.DataSource = G_st.Tables[0];//設置數據源
        }

        /// <summary>
        /// 查詢數據庫信息
        /// </summary>
        /// <returns>方法返回DataTable對象</returns>
        private void GetMessage()
        {
            G_st.Tables[0].Clear();
            string P_Str_ConnectionStr = string.Format(@"server=192.168.60.18;database=浙江先芯科技;uid=sa;pwd=lierda");
            string P_Str_SqlStr = string.Format("SELECT [MPN],[品牌],[結存數量],[貨位號],[ID] FROM [dbo].[備品]");
            SqlDataAdapter P_SqlDataAdapter = new SqlDataAdapter(//創建數據適配器
                P_Str_SqlStr, P_Str_ConnectionStr);
            P_SqlDataAdapter.Fill(G_st.Tables[0]);//填充數據表
        }

        private void dgv_Message_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            G_st.Tables[0].Rows[e.RowIndex][e.ColumnIndex] =dgv_Message.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
        }

    }
}

圖片說明

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved