已有56人关注
17章练习五datagriw输入数据出错
发表在C#图书答疑 2014-11-10
是否精华
版块置顶:
[strong]"对于不返回任何键列信息的 SelectCommand,不支持 UpdateCommand 的动态 SQL 生成。"}
[/strong]
这个问题要怎么处理?

请编写一下完整代码

我现在的代码是这样的:
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 _123456
{
    public partial class Form4 : Form
    {
        public Form4()
        {
            InitializeComponent();
        }
        SqlDataAdapter sda = null;
        public DataTable GetDataTable(string strSql)
        {
            string strConn = "Server=PC-201207261042\\SQLEXPRESS;Integrated Security=SSPI;database = 123456";
            SqlConnection sqlConn = new SqlConnection(strConn);
            try
            {
                sda = new SqlDataAdapter(strSql, sqlConn);
                new SqlCommandBuilder(sda);
                DataTable dt = new DataTable();
                sda.Fill(dt);
                return dt;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        private void Form4_Load(object sender, EventArgs e)
        {
            shiborBindingSource.DataSource = GetDataTable("Select * From shibor");
            dataGridView1.DataSource = shiborBindingSource;
            // TODO: 这行代码将数据加载到表“_123456DataSet.shibor”中。您可以根据需要移动或删除它。
            //this.shiborTableAdapter.Fill(this._123456DataSet.shibor);
            //this.WindowState = FormWindowState.Maximized;
        }

        private void Form4_MdiChildActivate(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {
            
        }

        private void button6_Click(object sender, EventArgs e)
        {
            dataGridView1.EndEdit();
            shiborBindingSource.EndEdit();
            DataTable dt = shiborBindingSource.DataSource as DataTable;

            try
            {
                if (sda.Update(dt) > 0)
                {
                    MessageBox.Show("保存成功!", "信息提示");
                }
                else
                {
                    MessageBox.Show("保存失败或数据未发生任何改变!", "信息提示");
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return;
        }
    }
}
分享到:
精彩评论 1
小科_mrkj
学分:43 LV2
2014-11-10
沙发
读者朋友:
    您好,new SqlCommandBuilder(sda);?这条语句应该是声明一个对象SqlCommandBuilder吧,另外,需要为shibor数据表设置一个主键列。
首页上一页 1 下一页尾页 1 条记录 1/1页
手机同步功能介绍
友情提示:以下图书配套资源能够实现手机同步功能
明日微信公众号
明日之星 明日之星编程特训营
客服热线(每日9:00-17:00)
400 675 1066
mingrisoft@mingrisoft.com
吉林省明日科技有限公司Copyright ©2007-2022,mingrisoft.com, All Rights Reserved长春市北湖科技开发区盛北大街3333号长春北湖科技园项目一期A10号楼四、五层
吉ICP备10002740号-2吉公网安备22010202000132经营性网站备案信息 营业执照