当前位置:编程学习 > C#/ASP.NET >>

C#中连接数据库后修改信息

 现已将需要修改的数据信息取出在文本框中,如下代码:
SqlConnection conn = new SqlConnection(WindowsFormsApplication1.Properties.Settings.Default.StudentsSysConnectionString);
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            conn.Open();
            if (stuno.Text == "")
                cmd.CommandText = id;
            else
                cmd.CommandText = "select * from StudentTable where stuno=" + stuno.Text.Trim();
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            sda.Fill(ds);
            if (ds.Tables[0].Rows.Count == 1)
            {
                this.xuehao.Text = (ds.Tables[0].Rows[0]["stuno"].ToString());
                this.xingming.Text = (ds.Tables[0].Rows[0]["stuname"].ToString());
                this.nianji.Text = (ds.Tables[0].Rows[0]["gradeno"].ToString());
                this.banji_.Text = (ds.Tables[0].Rows[0]["classno"].ToString());
                this.xingbie.Text = (ds.Tables[0].Rows[0]["stusex"].ToString());
                this.minzu.Text = (ds.Tables[0].Rows[0]["nation"].ToString());
                this.shenfenzheng.Text = (ds.Tables[0].Rows[0]["idcard"].ToString());
                this.dizhi.Text = (ds.Tables[0].Rows[0]["address"].ToString());
                conn.Close();
            }
然后进行修改,如下图:

点击确定修改后(xiugaibtn_Click)如下代码:
private void xiugaibtn_Click(object sender, EventArgs e)
        {
            if (xuehao.Text.Trim() == "" || xingming.Text.Trim() == "" || nianji.Text.Trim() == "" || banji_.Text.Trim() == ""
                || xingbie.Text.Trim() == "" || minzu.Text.Trim() == "" || shenfenzheng.Text.Trim() == ""|| dizhi.Text.Trim() == "")
            {
                MessageBox.Show("请输入完整信息!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                string str1 = xuehao.Text.Trim();
                int l = str1.Length;
                for (int i = 0; i < l; i++)
                {
                    if (!char.IsNumber(str1[i]))
                    {
                        MessageBox.Show("输入学号有误!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        xuehao.SelectAll();
                        xuehao.Focus();
                        return;
                    }
                }
              
                SqlConnection conn = new SqlConnection(WindowsFormsApplication1.Properties.Settings.Default.StudentsSysConnectionString);
                conn.Open();
                string sql = "update StudentTable set stuno='" + xuehao.Text + "',stuname='" + xingming.Text + "',gradeno='" + nianji.Text
                + "',classno='" + banji_.Text + "',stusex='" + xingbie.Text + "',nation='" + minzu.Text
                + "',idcard='" + shenfenzheng.Text + "',address='" + dizhi.Text + "'";
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.ExecuteNonQuery();
                connection.Close();
                xiugaixuesheng_Click(sender, e);
            }
        }
但是为什么数据库中的信息不能正确更改呢,在修改信息代码中是sql语句有问题吗?希望大家帮忙解决一下,谢谢啦! --------------------编程问答-------------------- 你把最终的sql放到sqlserver中执行看看 --------------------编程问答-------------------- 更新时没where 条件吗? --------------------编程问答--------------------

update语句忘记写where条件,这习惯得改呀

估计是你修改的语句没执行 --------------------编程问答-------------------- SQL语句不对哦,举个例子
UPDATE Person SET FirstName = 'Fred' WHERE LastName = 'Wilson'
--------------------编程问答-------------------- 设个断点调试一下看看你的SQL语句有没有问题,
可能会有""''这种标点弄错的时候。 --------------------编程问答--------------------
引用 3 楼 happy09li 的回复:


update语句忘记写where条件,这习惯得改呀

估计是你修改的语句没执行

我想可以把所有的信息都可以修改,where可以填什么条件呀.. --------------------编程问答--------------------
引用 6 楼 abc1874166 的回复:
Quote: 引用 3 楼 happy09li 的回复:



update语句忘记写where条件,这习惯得改呀

估计是你修改的语句没执行

我想可以把所有的信息都可以修改,where可以填什么条件呀..


那就不用加where条件了,你相加 就加where1=1 --------------------编程问答--------------------
引用 7 楼 happy09li 的回复:
那就不用加where条件了,你相加 就加where1=1

然后数据库就更新不出来了 --------------------编程问答--------------------
引用 8 楼 abc1874166 的回复:
Quote: 引用 7 楼 happy09li 的回复:


那就不用加where条件了,你相加 就加where1=1

然后数据库就更新不出来了


你打个断点把sql语句复制到sql客户端中执行看下,是否成功,
我怀疑你那个事件都没触发 --------------------编程问答--------------------
引用 6 楼 abc1874166 的回复:
Quote: 引用 3 楼 happy09li 的回复:



update语句忘记写where条件,这习惯得改呀

估计是你修改的语句没执行

我想可以把所有的信息都可以修改,where可以填什么条件呀..


你的学号应该是关键字,不能修改才对!要不然你后面什么都做不了。 --------------------编程问答--------------------
引用 9 楼 happy09li 的回复:
你打个断点把sql语句复制到sql客户端中执行看下,是否成功,
我怀疑你那个事件都没触发

where已经加上了,但是现在查询功能可以,修改和添加功能的数据库都出不来。下面的添加代码有问题吗?
string[] strInfo = new string[10];
            strInfo[0] = stutext.Text;
            strInfo[1] = nametext.Text;
            strInfo[2] = zhuanyetext.Text;
            strInfo[3] = banjitext.Text;
            strInfo[4] = mingchengtext.Text;
            strInfo[5] = dengjitext.Text;
            strInfo[6] = danweitext.Text;
            strInfo[7] = jibietext.Text;
            strInfo[8] = shijiantext.Text;
            if (strInfo[0] != "")
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.CommandType = System.Data.CommandType.Text;
                cmd.CommandText = "insert into huojiangTable(学号,获奖者,获奖者专业,班级,赛事名称,获奖等级,主办国家和单位,赛事级别,获奖时间) values('" + strInfo[0] + "','" + strInfo[1] + "','" + strInfo[2] + "','" + strInfo[3] + "','" + strInfo[4] + "','" + strInfo[5] + "','" + strInfo[6] + "','" + strInfo[7] + "','" + strInfo[8] + "')";
                cmd.ExecuteNonQuery();
                conn.Close();
            }
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,