http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 2241|回复: 4

[其他] 关于c#的数据库基本增删查改的操作

[复制链接]
发表于 2013-8-22 11:38:18 | 显示全部楼层 |阅读模式


namespace chaxun3
{
    public partial class Form2 : Form
    {
        private DataGridViewRow _row;
        public Form2()
        {
            InitializeComponent();
        }



        private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("server=192.168.1.51;uid=sa;password=sa@123456;Database=StudentSystem");
            string str1 = "select * from M_STUDENT ";
            con.Open();
            SqlDataAdapter adapter = new SqlDataAdapter(str1, con);//讲查询到的数据输出

            DataSet DS = new DataSet();//创建临时记录表
            adapter.Fill(DS, "M_STUDENT");//填充数ju
            DataTable mytable = DS.Tables[0];

         mytable.Rows[0][0] = textBox1.Text ;
            mytable.Rows[0][1] = textBox2.Text ;
            mytable.Rows[0][2] = textBox3.Text ;
            mytable.Rows[0][3] = textBox4.Text ;
            mytable.Rows[0][4] = textBox5.Text ;
            mytable.Rows[0][5] = textBox6.Text ;
            mytable.Rows[0][6] = textBox7.Text ;

                SqlCommand cmd = new SqlCommand("Update  M_STUDENT  ", con);


              SqlCommandBuilder sb = new SqlCommandBuilder(adapter);
               adapter.Update(mytable);
                MessageBox.Show("保存成功");
                Form1 frm = new Form1();
                frm.ShowDialog();
                this.Close();

                con.Close();
            }






        private void Form2_Load(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("server=192.168.1.51;uid=sa;password=sa@123456;Database=StudentSystem");//数据库连接
            con.Open();
            string str = "select * from  M_STUDENT ";  //查询的数据内容
            SqlDataAdapter adapter = new SqlDataAdapter(str, con);//讲查询到的数据输出
            DataSet DS = new DataSet();//创建临时记录表
            adapter.Fill(DS, "M_STUDENT");//填充数ju


        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        public void Getrow(DataGridViewRow row)
        {
            this.textBox1.Text = row.Cells["学号"].Value.ToString();
            this.textBox2.Text = row.Cells["姓名"].Value.ToString();
            this.textBox3.Text = row.Cells["性别"].Value.ToString();
            this.textBox4.Text = row.Cells["电话"].Value.ToString();
            this.textBox5.Text = row.Cells["地址"].Value.ToString();
            this.textBox6.Text = row.Cells["生日"].Value.ToString();
            this.textBox7.Text = row.Cells["备注"].Value.ToString();


        }
    }
}
报错:制約 'PK_M_STUDENT' の PRIMARY KEY 違反。オブジェクト 'dbo.M_STUDENT' には重複したキーを挿入できません。
ステートメントは終了されました。
提问:编译执行时只能修改更新数据库的第一行数据,改其他数据行的时候就报错了,我该怎么修改呢?




1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2013-8-22 12:53:23 | 显示全部楼层


namespace chaxun3
{
    public partial class Form2 : Form
    {
        private DataGridViewRow _row;
        public Form2()
        {
            InitializeComponent();
        }



        private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("server=192.168.1.51;uid=sa;password=sa@123456;Database=StudentSystem");
            string str1 = "select * from M_STUDENT ";
            con.Open();
            SqlDataAdapter adapter = new SqlDataAdapter(str1, con);//讲查询到的数据输出

            DataSet DS = new DataSet();//创建临时记录表
            adapter.Fill(DS, "M_STUDENT");//填充数ju
            DataTable mytable = DS.Tables[0];

         mytable.Rows[0][0] = textBox1.Text ;
            mytable.Rows[0][1] = textBox2.Text ;
            mytable.Rows[0][2] = textBox3.Text ;
            mytable.Rows[0][3] = textBox4.Text ;
            mytable.Rows[0][4] = textBox5.Text ;
            mytable.Rows[0][5] = textBox6.Text ;
            mytable.Rows[0][6] = textBox7.Text ;

                SqlCommand cmd = new SqlCommand("Update  M_STUDENT  ", con);


              SqlCommandBuilder sb = new SqlCommandBuilder(adapter);
               adapter.Update(mytable);
                MessageBox.Show("保存成功");
                Form1 frm = new Form1();
                frm.ShowDialog();
                this.Close();

                con.Close();
            }






        private void Form2_Load(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("server=192.168.1.51;uid=sa;password=sa@123456;Database=StudentSystem");//数据库连接
            con.Open();
            string str = "select * from  M_STUDENT ";  //查询的数据内容
            SqlDataAdapter adapter = new SqlDataAdapter(str, con);//讲查询到的数据输出
            DataSet DS = new DataSet();//创建临时记录表
            adapter.Fill(DS, "M_STUDENT");//填充数ju


        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        public void Getrow(DataGridViewRow row)
        {
            this.textBox1.Text = row.Cells["学号"].Value.ToString();
            this.textBox2.Text = row.Cells["姓名"].Value.ToString();
            this.textBox3.Text = row.Cells["性别"].Value.ToString();
            this.textBox4.Text = row.Cells["电话"].Value.ToString();
            this.textBox5.Text = row.Cells["地址"].Value.ToString();
            this.textBox6.Text = row.Cells["生日"].Value.ToString();
            this.textBox7.Text = row.Cells["备注"].Value.ToString();


        }
    }
}
报错:制約 'PK_M_STUDENT' の PRIMARY KEY 違反。オブジェクト 'dbo.M_STUDENT' には重複したキーを挿入できません。
ステートメントは終了されました。
提问:编译执行时只能修改更新数据库的第一行数据,改其他数据行的时候就报错了,我该怎么修改呢?


你这是什么语言啊看不懂,不过看着像是主键冲突,有可能是学号重复了,不能写入,
你看看是不是写入的学号已存在了。
发表于 2013-8-22 14:09:46 | 显示全部楼层
本帖最后由 970443232 于 2013-8-22 14:10 编辑

前段时间研究过,最近重装没弄数据库,我对这个不熟悉,不能帮你,我有个sqlhelper 类你可以使用,另外建议楼主学下数据库的基本增删改查

传智播客的基础视频不错,声明不是打广告,

下边是我前段时间看视频 学习的不过现在都忘了,应该能运行,数据库连接在  app.config  而且都有注释
复习三层 ListUI和EditUI VS2010.rar (100.98 KB, 下载次数: 122)
 楼主| 发表于 2013-8-22 14:36:19 | 显示全部楼层
站长苏飞 发表于 2013-8-22 12:53
namespace chaxun3
{
    public partial class Form2 : Form

那我该怎么检查是否写入了呢
 楼主| 发表于 2013-8-22 14:38:07 | 显示全部楼层
站长苏飞 发表于 2013-8-22 12:53
namespace chaxun3
{
    public partial class Form2 : Form

哎,我还是不懂呀。。。。。。
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-5-7 11:28

© 2014-2021

快速回复 返回顶部 返回列表