| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 715 人关注过本帖
标题:c# 中进行数据库操作出错,请问哪里有误??
取消只看楼主 加入收藏
讨厌数构
Rank: 1
等 级:新手上路
帖 子:32
专家分:4
注 册:2013-10-21
结帖率:50%
收藏
 问题点数:0 回复次数:4 
c# 中进行数据库操作出错,请问哪里有误??
private void btnAdd_Click(object sender, EventArgs e)//有误
        {
            if (sqlCon.State == ConnectionState.Open)
                sqlCon.Close();
            sqlCon.Open();
            if (this.txtSid.Text == "")
            {
                MessageBox.Show("请输入学号");
                this.txtSid.Focus();
                return;
            }
             = "select count(*) from student where Sid='" + this.txtSid.Text + "'";
            sqlCmd.Connection = sqlCon;
            int i = Convert.ToInt32(sqlCmd.ExecuteScalar());
            if (i > 0)
            {
                MessageBox.Show("该学号已经存在,请重新输入!");
                this.txtSid.SelectionStart = 0;
                this.txtSid.SelectionLength = this.txtSid.Text.Length;
                this.txtSid.Focus();
                return;
            }//判断学号

            if (this.txtSname.Text == "")
            {
                MessageBox.Show("请输入姓名!");
                this.txtSname.Focus();
                return;

            }//判断姓名

            if (this.txtSphone.Text != "")
            {
                if (this.IsNumber(this.txtSphone.Text) == false)
                {
                    MessageBox.Show("电话号码应该输入数字!");
                    this.txtSphone.SelectionStart = 0;
                    this.txtSphone.SelectionLength = this.txtSphone.Text.Length;
                    this.txtSphone.Focus();
                    return;
                }
            }//判断电话号码

            string sfex = "";
            sqlCmd.Connection = sqlCon;
            if (this.rdbMan.Checked == true)
                sfex = "男";
            else
                sfex = "女";
            string strAdd = "insert into student(Sname,Sfex,Sid,Sphonenumber,Sbirthday)values('" + this.txtSname.Text + "','" + sfex + "','" + this.txtSid.Text + "','" + this.txtSphone.Text + "','" + this.txtSbirthday.Text + "')";
             = strAdd;//哪里错了???
            sqlCmd.ExecuteNonQuery();
            MessageBox.Show("成功!");

           /* = "insert into student(Sname,Sfex,Sid,Sphonenumber,Sbirthday)values('" + this.txtSname.Text + "','" + sfex + "','" + this.txtSid.Text + "','" + this.txtSphone.Text + "','" + this.txtSbirthday.Text + "')";
            sqlCmd.ExecuteNonQuery();
            MessageBox.Show("学生记录添加成功!");
            * */
            /*
             *   = "insert into stu values('" + this.txtID.Text + "','" + this.txtName.Text + "','" + stuSex + "','" + dtpBirth.Text + "','" + this.txtPhone.Text + "')";
                sqlCmd.ExecuteNonQuery();
            //   MessageBox.Show("学生记录添加成功!");
             * */
            this.clear();
            this.fillGrid();
        }

        public bool IsNumber(string str){
            try{
                long var1 = Convert.ToInt64(str);
                return true;
            }
            catch{
            return false;
            }
        }//判断是否是数字

        public void fillGrid(){
            this.listView1.Items.Clear();
            if(sqlCon.State==ConnectionState.Open)
                sqlCon.Close();
            sqlCon.Open();

            sqlCmd.Connection=sqlCon;
             * from student";
            SqlDataReader sdr=sqlCmd.ExecuteReader();
            while(sdr.Read())
            {
                ListViewItem lv=new ListViewItem(sdr[3].ToString());
                lv.SubItems.Add(sdr[0].ToString());
                lv.SubItems.Add(sdr[2].ToString());
                lv.SubItems.Add(sdr[4].ToString());
                lv.SubItems.Add(sdr[5].ToString());
                listView1.Items.Add(lv);
                //添加一行

            }
        }
        //添加


程序运行时说
   string strAdd = "insert into student(Sname,Sfex,Sid,Sphonenumber,Sbirthday)values('" + this.txtSname.Text + "','" + sfex + "','" + this.txtSid.Text + "','" + this.txtSphone.Text + "','" + this.txtSbirthday.Text + "')";
             = strAdd;//哪里错了???
            sqlCmd.ExecuteNonQuery();
            MessageBox.Show("成功!");
有错,可是不知道哪里错了,可以帮忙解答一下吗??

数据库如下:
create database a;
create table student
(
Sname  varchar(10),
Spwd  varchar(20),
Sfex  varchar(4),
Sid varchar(20),
Sphonenumber varchar(15),
Sbirthday varchar(20)

)


select  * from  student
insert into student values('huangli','123','女','1225112009','18850571140','1994-11-02')
drop table student
搜索更多相关主题的帖子: 数据库操作 private return count where 
2014-04-23 11:03
讨厌数构
Rank: 1
等 级:新手上路
帖 子:32
专家分:4
注 册:2013-10-21
收藏
得分:0 
回复 3 楼 步向巅峰
把C#中的sql语句直接拷贝到SQLServer里面就能看出来了

一步一步好好学下去吧。加油!!!
2014-04-30 10:34
讨厌数构
Rank: 1
等 级:新手上路
帖 子:32
专家分:4
注 册:2013-10-21
收藏
得分:0 
请问怎么结贴呀??各位??

一步一步好好学下去吧。加油!!!
2014-04-30 10:42
讨厌数构
Rank: 1
等 级:新手上路
帖 子:32
专家分:4
注 册:2013-10-21
收藏
得分:0 
图片附件: 游客没有浏览图片的权限,请 登录注册

没有啊,亲

一步一步好好学下去吧。加油!!!
2014-04-30 15:57
讨厌数构
Rank: 1
等 级:新手上路
帖 子:32
专家分:4
注 册:2013-10-21
收藏
得分:0 
什么是问题点数呀??

一步一步好好学下去吧。加油!!!
2014-04-30 17:13
快速回复:c# 中进行数据库操作出错,请问哪里有误??
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.032845 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved