已有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-31
11L
读者朋友:
    您好,这个跟VS2005还是2008没有关系,但数据库可能不一样,因为从2005转成2000之后,有的数据表中的自动标识的字段就没有了,得手动设置一下,程序中的代码是没有问题的。
首页上一页 12 下一页尾页 11 条记录 2/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经营性网站备案信息 营业执照