weixin_45796800 2020-06-29 22:15 采纳率: 100%
浏览 878
已采纳

C# winform datagridview向SQL数据库中添加多行数据

我想将datagridview中的多行数据添加到SQL数据库中,但是总是显示“添加失败”,而且添加后查看数据库,只有最后一行数据库加进去了。
求解答,我的头快秃了......(ㄒoㄒ)
我们没有学过多行数据的,我只会单行添加,在网上找了很多资料也没有办法。
上学期我们老师讲的C#简直就是一坨shit,全靠自学,现在又来布置作业,枯了

dim_2是我的数据表

private void 保存转换后坐标ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //连接数据库
            string connStr = @"Data Source=8RLL4E2HQZ2PO3V;Initial Catalog=Internship;Integrated Security=True";

            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = connStr;
            conn.Open();
            //创建SQL命令
            SqlCommand cmd = new SqlCommand();
            for (int i = 0; i < Pnum; i++)
            {
                string sql = "insert into dim_2 values(" + this.dataGridView2.Rows[i].Cells[0].Value.ToString() + "," +
                this.dataGridView2.Rows[i].Cells[1].Value + "," + this.dataGridView2.Rows[i].Cells[2].Value + "," +
                this.dataGridView2.Rows[i].Cells[3].Value + "," + this.dataGridView2.Rows[i].Cells[4].Value + "," +
                this.dataGridView2.Rows[i].Cells[5].Value + "," + this.dataGridView2.Rows[i].Cells[6].Value + ")";
                cmd.CommandText = sql;
                cmd.Connection = conn;
            }

            int row = cmd.ExecuteNonQuery();

            conn.Close();
            conn.Dispose();

            if (row == 3)
                MessageBox.Show("添加成功!");

            else
                MessageBox.Show("添加失败!");

        }
  • 写回答

3条回答 默认 最新

  • Json-Huang 2020-06-29 23:39
    关注

    代码有两个地方有问题,for循环最终输出的sql是一条记录的脚本,row是执行的影响行数,row==3改成row>0即可

    可以for循环构造insert into批量插入语句,
    语法类似:INSERT INTO table_name (column1,column2,column3,...)
    VALUES (value1,value2,value3,...),
     (value1,value2,value3,…),
     ...
     (value1,value2,value3,…);
    
    伪代码参考如下:
    string sql="insert into dim_2 values";
    for (int i = 0; i < Pnum; i++)
                {
                    sql += "(" + this.dataGridView2.Rows[i].Cells[0].Value.ToString() + "," +
                    this.dataGridView2.Rows[i].Cells[1].Value + "," + this.dataGridView2.Rows[i].Cells[2].Value + "," +
                    this.dataGridView2.Rows[i].Cells[3].Value + "," + this.dataGridView2.Rows[i].Cells[4].Value + "," +
                    this.dataGridView2.Rows[i].Cells[5].Value + "," + this.dataGridView2.Rows[i].Cells[6].Value + “),”;
    
                }
          sql=sql.Substring(0,sql.Length-1);//移除最后的,
          cmd.CommandText = sql;
          cmd.Connection = conn;
          conn.Close();
          conn.Dispose();
    
                if (row >0)
                    MessageBox.Show("添加成功!");
                else
                    MessageBox.Show("添加失败!");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!