已有56人关注
帮我看看
发表在C#图书答疑 2010-09-27
是否精华
版块置顶:
  try 
            {
              SqlConnection  conn=new SqlConnection ();
              conn.ConnectionString =" Server=PC-200907140942\\SA;uid=sa;pwd=sa; database=db_11 ";
                conn.Open ();
                string SqlIns = "Insert into tb_11 values('" + txtId.Text + "','" + txtName.Text + "' ,'" + txtSex.Text + "','" + txtTell.Text + "')";
                //string SqlIns = "Insert into tb_11 values('" + txtId.Text + "','"  +txtName.Text + "' ) ";
                SqlCommand command=new SqlCommand ();
                command .CommandText =SqlIns ;
                command .Connection =conn;
                command .ExecuteNonQuery ();
                MessageBox .Show ("数据添加成功!");
                SqlDataAdapter  AdapterSelect=new SqlDataAdapter ("select * from tb_11",conn );
                DataTable  dt=new DataTable ();
                AdapterSelect .Fill (dt );
                //dataGridView1.DataSource = dt.DefaultView();
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message.ToString());
            }
    这个代码有没有错误,添加不成功。如果dataGridView1.DataSource = dt.DefaultView();这个没有加注释就会出现错误提示,错误 “ Non-invocable member 'System.Data.DataTable.DefaultView' cannot be used like a method. ”去掉就可以,但运行结果显示不出。提示“仅当使用了列的列表,并且IDENTITY_INSERT为ON时,才能在表'tb_11'中为标识列指定显式值。”这个,在C#程序设计标准教程书本P272的例子,我只不过把里面的表改了一下。谢谢!!!
分享到:
精彩评论 1
小科_mrkj
学分:43 LV2
2010-09-27
沙发
读者朋友:
    您好,将SqlDataAdapter AdapterSelect=new SqlDataAdapter ("select * from tb_11",conn );
DataTable dt=new DataTable ();
AdapterSelect .Fill (dt );
//dataGridView1.DataSource = dt.DefaultView();
修改为:
SqlDataAdapter AdapterSelect=new SqlDataAdapter ("select * from tb_11",conn );
DataSet dt=new DataSet();
AdapterSelect .Fill (dt );
dataGridView1.DataSource = dt.Tables[0];
首页上一页 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经营性网站备案信息 营业执照