已有56人关注
SqlDataAdaptert和添加数据
发表在C#图书答疑 2009-05-26
是否精华
版块置顶:
      using System.Data.SqlClient;
     //SqlDataAdapter使用
       SqlConnection con = new SqlConnection();
            con.ConnectionString = "Server=.;uid=sa;pwd=sa;database=db_PMS";
            con.Open();
            DataTable dt = new DataTable("Resouce");
            string SqlIns = "insert into tb_user values('" + textBox1.Text + " ','" + textBox2.Text + "')";
            SqlCommand command = new SqlCommand(SqlIns, con);
            SqlDataAdapter adapter = new SqlDataAdapter();
            adapter.SelectCommand = command;
            adapter.Fill(dt);
            dt.Clear();
            SqlDataAdapter AdapterSelect = new SqlDataAdapter("select * from tb_User", con);
            AdapterSelect.Fill(dt);
            dataGridView1.DataSource = dt.DefaultView;
    我运行的时候,提示adapter.Fill(dt);出现异常 , DataTable dt = new DataTable("Resouce");这条语句什么意思不明白。Resouce 是什么意思?


       //添加

              using System.Data.SqlClient;

try
            {
                SqlConnection conn = new SqlConnection();
                conn.ConnectionString = "Server=.;uid=sa;pwd=sa;database=db_PMS";
                conn.Open();
                string SqlIns = "insert into tb_department values('" + textBox1.Text + "','" + textBox2.Text + "')";
                SqlCommand command = new SqlCommand();
                command.CommandText = SqlIns;
                command.Connection = conn;
                command.ExecuteNonQuery();
                MessageBox.Show("数据库添加成功!");
                SqlDataAdapter AdapterSelect = new SqlDataAdapter("select * from tb_department", conn);
                DataTable dt = new DataTable();
                AdapterSelect.Fill(dt);
                dataGridView1.DataSource = dt.DefaultView;

            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message.ToString());
            }

        运行没有反应







分享到:
精彩评论 11
小科_mrkj
学分:43 LV2
2009-05-26
沙发
读者朋友:
    您好,请问这是哪本书的哪个程序。
坚持就是胜利
学分:0 LV1
2009-05-26
板凳
   C#程序设计标准教程
小科_mrkj
学分:43 LV2
2009-05-26
地板
读者朋友:
    您好,问题回复如下:
    (1)Resouce是自己随便起的一个表名,这里面可以没有,你最好插入断点查看一下具体出现什么错误。
    (2)你从开始插入断点看一下走到那开始没有反应的。
    程序调试过程中,使用VS提供的插入断点功能可以解决很多问题,建议多用一下。
坚持就是胜利
学分:0 LV1
2009-05-27
4L
  我试过插入断点在这个语句上 adapter.Fill(dt);出现异常“仅当使用了列的列表,并且 IDENTITY_INSERT 为 ON 时,才能在表 'tb_user' 中为标识列指定显式值。”我看不明白,下面的添加这一段语句运行也一样出现这个情况
小科_mrkj
学分:43 LV2
2009-05-27
5L
读者朋友:
    您好,把insert into tb_user values('" + textBox1.Text + " ','" + textBox2.Text + "')"这个SQL语句写全,即insert into tb_user(表里面的字段名) values('" + textBox1.Text + " ','" + textBox2.Text + "')",然后再试试。
坚持就是胜利
学分:0 LV1
2009-05-28
6L
  我回去试过了,还是不行,仍然出现这个问题。
小科_mrkj
学分:43 LV2
2009-05-30
7L
读者朋友:
    您好,您再检查一下数据库,看一下tb_User和tb_department这两个数据表里的id字段是不是int类型的,如果是int类型,它们应该是自动添加的,即在SQL企业管理器中数据表的下面有一个“自动标识”选项,你把它选为“是”,然后保存再运行程序看一下。
坚持就是胜利
学分:0 LV1
2009-05-31
8L
 P272页我只做了一个添加,但不能运行。我把整个代码复制过来了。图片弄不上去     
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WeExample
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection conn = new SqlConnection();
                conn.ConnectionString = "Server=WWW-7BB70BBA180;uid=sa;pwd=sa;database=db_PMS";
                conn.Open();
                string SqlIns = "insert into tb_userJob values('" + textBox1.Text + "','" + textBox2.Text + " ')";
                SqlCommand command = new SqlCommand();
                command.CommandText = SqlIns;
                command.Connection = conn;
                command.ExecuteNonQuery();
                MessageBox.Show("数据库添加成功!");
                SqlDataAdapter AdapterSelect = new SqlDataAdapter("select * from tb_userJob", conn);
                DataTable dt = new DataTable();
                AdapterSelect.Fill(dt);
                dataGridView1.DataSource = dt.DefaultView;
            }
            catch(Exception ee)
            {
                
                MessageBox.Show(ee.Message.ToString());
            

            }
        }

        private void Form3_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“db_PMSDataSet.tb_userJob”中。您可以根据需要移动或移除它。
            this.tb_userJobTableAdapter.Fill(this.db_PMSDataSet.tb_userJob);

        }
    }
}
     还是显示上次的“IDENTITY_INSERT 为 ON 时,才能在表 'tb_user' 中为标识列指定显式值”这个问题
小科_mrkj
学分:43 LV2
2009-05-31
9L
读者朋友:
    您好,您再看一下我们光盘中的源程序,看能否正确运行,注意把从光盘中拷出来的文件夹的只读属性去掉,这个程序在我们这里是没有错误的,您再试一下源程序。
坚持就是胜利
学分:0 LV1
2009-05-31
10L
           我的版本是vc#2005和sql2000,光盘的代码是要用2008和sql2005的吗,没有办法运行
首页上一页 12 下一页尾页 11 条记录 1/2页
手机同步功能介绍
友情提示:以下图书配套资源能够实现手机同步功能
明日微信公众号
明日之星 明日之星编程特训营
客服热线(每日9:00-17:00)
400 675 1066
mingrisoft@mingrisoft.com
吉林省明日科技有限公司Copyright ©2007-2022,mingrisoft.com, All Rights Reserved长春市北湖科技开发区盛北大街3333号长春北湖科技园项目一期A10号楼四、五层
吉ICP备10002740号-2吉公网安备22010202000132经营性网站备案信息 营业执照