Csharpsss
Csharpsss
采纳率88.2%
2018-06-09 08:25 阅读 1.2k

将datagrideview中编辑的数据保存到sql 要源码!!!

80

![图片说明](https://img-ask.csdn.net/upload/201806/09/1528532532_852185.png)图片说明

不要用linq to SQL 链接数据库打开 foreach(datagrideview)....保存到数据库...清空当前datagridview内容.......

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

3条回答 默认 最新

  • 已采纳
    showbo GoCityPass新加坡曼谷通票 2018-06-09 12:28
    点赞 5 评论 复制链接分享
  • dabocaiqq dabocaiqq 2018-06-09 09:10

    图片说明

     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 Q691608
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=Q691608DB;Integrated Security=True;Pooling=False");
                conn.Open();
                foreach (DataGridViewRow row in dataGridView1.Rows)
                {
                    try
                    {
                        string sql = string.Format("insert into table1(姓名,工号,邮箱) values('{0}','{1}','{2}')", row.Cells[0].Value.ToString(), row.Cells[1].Value.ToString(), row.Cells[2].Value.ToString());
                        SqlCommand cmd = new SqlCommand(sql, conn);
                        cmd.ExecuteScalar();
                    }
                    catch { }
                }
                dataGridView1.Rows.Clear();
            }
        }
    }
    
    
    点赞 评论 复制链接分享
  • dabocaiqq dabocaiqq 2018-06-09 09:18
    点赞 评论 复制链接分享

相关推荐