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

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

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

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

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

查看: 3249|回复: 3

[学生提问] 大神帮忙看看c#添加、修改在一个界面

[复制链接]
发表于 2015-4-20 22:29:31 | 显示全部楼层 |阅读模式
1金钱
QQ截图20150420222145.png QQ图片20150420221934.png 请帮帮忙,C#中添加、修改在一个界面有两处错误,下面是代码:

[C#] 纯文本查看 复制代码
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 stu
{
    public partial class user_info : Form
    {
        public user_info()
        {
            InitializeComponent();
            
       }
        SqlConnection conn;
        SqlCommand cmd;
        DataSet ds;
        SqlDataAdapter sda;
     
        public void user_info_Load(object sender, EventArgs e)
        {
            conn = stu.DBconn.stucon();
            string str = "select * from [user]";
            cmd = new SqlCommand(str, conn);
            sda = new SqlDataAdapter();
            sda.SelectCommand = cmd;
            ds = new DataSet();
            sda.Fill(ds, "fs");
            dataGridView1.DataSource = ds.Tables[0];
        }

        public void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            textBox1.Text = dataGridView1.SelectedCells[0].Value.ToString();
            textBox2.Text = dataGridView1.SelectedCells[1].Value.ToString();
            textBox3.Text = dataGridView1.SelectedCells[2].Value.ToString();
        }

        public void user_info_Load_1(object sender, EventArgs e)
        {
            this.userTableAdapter.Fill(this.mydataDataSet.user);
        }

        //删除
        private void bt_shanchu_Click(object sender, EventArgs e)
        {
            conn = stu.DBconn.stucon();
            conn.Open();
            cmd = new SqlCommand("select * from [user] where 用户名='" + textBox1.Text + "'",conn);
            int i = Convert.ToInt32(cmd.ExecuteScalar());
            if (textBox1.Text != "")
            {
                if (i != 0)
                {
                    //string id = dataGridView1.SelectedCells[0].Value.ToString();
                    cmd = new SqlCommand("delete from [user] where 用户名='" + textBox1.Text + "'", conn);
                    int j = Convert.ToInt32(cmd.ExecuteScalar());
                    if (j > 0)
                    {
                        MessageBox.Show("用户信息删除失败!");
                    }
                    else
                    {
                        MessageBox.Show("用户信息删除成功!");
                        conn.Close();
                    }
                }
                else
                {
                    MessageBox.Show("没有该用户记录,请重新输入!");
                    textBox1.Focus();
                }
            }
            else
            {
                MessageBox.Show("请输入待删除用户信息!");
                textBox1.Focus();
            }
            this.dataGridView1.DataSource = null;
            string str = "select * from [user]";
            cmd = new SqlCommand(str,conn);
            sda = new SqlDataAdapter();
            sda.SelectCommand = cmd;
            ds = new DataSet();
            sda.Fill(ds,"fs");
            dataGridView1.DataSource = ds.Tables[0];
            textBox1.Text = "";
        }

        //添加
        private void bt_tianjia_Click(object sender, EventArgs e)
        {
            
            conn = stu.DBconn.stucon();
            conn.Open();
            cmd = new SqlCommand("select count(*) from [user] where 用户名='" + textBox1.Text + "'", conn);
            int i = Convert.ToInt32(cmd.ExecuteScalar());
            if (i == 0)
            {
                sda = new SqlDataAdapter("select * from [user]", conn);
                SqlCommandBuilder sbuildr = new SqlCommandBuilder(sda);
                sda.Fill(ds, "[user]");
                DataRow dr = ds.Tables["user"].NewRow();
                dr["用户名"] = textBox1.Text.Trim();
                dr["姓名"] = textBox2.Text.Trim();
                dr["密码"] = textBox3.Text.Trim();
               
                ds.Tables["user"].Rows.Add(dr);
                sda.Update(ds, "[user]");
                MessageBox.Show("记录添加成功!");
                this.dataGridView1.DataSource = null;
                SqlDataAdapter ssda = new SqlDataAdapter();
                SqlCommand scmd = new SqlCommand("select * from [user]", conn);
                ssda.SelectCommand = scmd;
                ds = new DataSet();
                ssda.Fill(ds, "fs");
                dataGridView1.DataSource = ds.Tables[0];
            }
            else
            {
                MessageBox.Show("已存在该记录");
                textBox1.Focus();
            }
        }

        //修改
        private void bt_xiugai_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != "")
            {
                DataTable dt = ds.Tables["fs"];
                sda = new SqlDataAdapter("select * from [user]", conn);
                sda.FillSchema(dt, SchemaType.Mapped);
                DataRow dr = dt.Rows.Find(textBox1.Text);
                dr["用户名"] = textBox1.Text.Trim();
                dr["姓名"] = textBox2.Text.Trim();
                dr["密码"] = textBox3.Text.Trim();
                SqlCommandBuilder sbuilder = new SqlCommandBuilder(sda);
                sda.Update(dt);
                MessageBox.Show("记录修改成功!");
                this.dataGridView1.DataSource = null;
                SqlDataAdapter ssda = new SqlDataAdapter();
                SqlCommand scmd = new SqlCommand("select * from [user]", conn);
                ssda.SelectCommand = scmd;
                ds = new DataSet();
                ssda.Fill(ds, "fs");
                dataGridView1.DataSource = ds.Tables[0];
            }
            else
            {
                MessageBox.Show("请输入用户信息!");
                textBox1.Focus();
            }

        }

    }
        }



1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2015-4-21 09:50:38 | 显示全部楼层
DataTable dt = ds.Tables["fs"];在取之前要先获取
回复

使用道具 举报

 楼主| 发表于 2015-4-21 11:26:37 | 显示全部楼层
站长苏飞 发表于 2015-4-21 09:50
DataTable dt = ds.Tables["fs"];在取之前要先获取

亲,我没有看懂。先获取应该在哪里加代码,加森么代码
回复

使用道具 举报

发表于 2015-4-22 08:24:29 | 显示全部楼层
DataTable dt = ds.Tables["fs"]
改为DataTable dt = ds.Tables[0] 系统会自动默认为第一个表
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

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

GMT+8, 2024-5-3 10:33

© 2014-2021

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