| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 949 人关注过本帖
标题:C#修改数据
只看楼主 加入收藏
zx10283
Rank: 2
等 级:论坛游民
帖 子:25
专家分:82
注 册:2011-10-20
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:3 
C#修改数据
我用下面程序用于修改数据库中的数据 ,但是却达不到效果,有高手请指导下

try
            {
                if (this.textBox1 .Text =="")
                {
                    MessageBox .Show ("请输入要修改的联系人的姓名!");
                }
                else
                {
                   string sqlconn = "Data Source=.;Initial Catalog=telephone;Integrated Security=True";
                   string sql;
                   sql =string .Format ( "select * from telephone where name='"+this.textBox1 .Text.Trim ()+"'");
                   SqlConnection conn = new SqlConnection(sqlconn);
                   SqlDataAdapter da = new SqlDataAdapter(sql,sqlconn);
                   DataSet ds = new DataSet();
                   da.Fill(ds,"telephone");
                    
                   if (ds.Tables [0].Rows.Count >0)
                   {
                       sql = string.Format(@"update telephone set mb='{3}',ph='{4}',unit='{5}',addr='{6}'where name='{1}'",this .textBox2 .Text .Trim (),
                           this.textBox3.Text.Trim(), this.textBox4.Text.Trim(), this.textBox5.Text.Trim(), this.textBox1.Text.Trim());
                       SqlConnection con = new SqlConnection(sqlconn);
                       SqlCommand cmd = new SqlCommand(sql ,con);
                       try
                       {
                           con.Open();
                           cmd.ExecuteNonQuery();
                       }
                       catch
                       { }
                       finally
                       {
                           con.Close();
                           con.Dispose();
                           cmd.Dispose();
                       }
                       MessageBox.Show("修改成功!");
                             //刷新数据
                       DataSet dss = new DataSet();
                       sql = "select * from telephone order by id";
                       SqlDataAdapter sda = new SqlDataAdapter(sql,conn);
                       da.Fill(ds,"telephone");
                       if (ds.Tables["telephone"].Rows.Count > 0)
                       {
                           this.dataGridView1.DataSource = ds.Tables["telephone"];
                       }
                   }
                }
            }
            catch
            {}
            
搜索更多相关主题的帖子: sql 数据库 telephone 联系人 where 
2011-10-21 20:25
zzqqrr
Rank: 6Rank: 6
来 自:299294817
等 级:侠之大者
威 望:1
帖 子:155
专家分:408
注 册:2011-6-11
收藏
得分:0 
你可以查到以后直接删除,再插入啊

59izk,com神奇百货,神奇百货网官网,shenqi,一个神奇的导购站!!
2011-10-21 22:43
girl0001
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:56
专家分:118
注 册:2010-7-17
收藏
得分:5 
以下是引用zx10283在2011-10-21 20:25:30的发言:

我用下面程序用于修改数据库中的数据 ,但是却达不到效果,有高手请指导下

try
            {
                if (this.textBox1 .Text =="")
                {
                    MessageBox .Show ("请输入要修改的联系人的姓名!");
                }
                else
                {
                   string sqlconn = "Data Source=.;Initial Catalog=telephone;Integrated Security=True";
                   string sql;
                   sql =string .Format ( "select * from telephone where name='"+this.textBox1 .Text.Trim ()+"'");
                   SqlConnection conn = new SqlConnection(sqlconn);
                   SqlDataAdapter da = new SqlDataAdapter(sql,sqlconn);
                   DataSet ds = new DataSet();
                   da.Fill(ds,"telephone");
                    
                   if (ds.Tables [0].Rows.Count >0)
                   {
                       sql = string.Format(@"update telephone set mb='{3}',ph='{4}',unit='{5}',addr='{6}'where name='{1}'",this .textBox2 .Text .Trim (),
                           this.textBox3.Text.Trim(), this.textBox4.Text.Trim(), this.textBox5.Text.Trim(), this.textBox1.Text.Trim());
                       SqlConnection con = new SqlConnection(sqlconn);
                       SqlCommand cmd = new SqlCommand(sql ,con);
                       try
                       {
                           con.Open();
                           cmd.ExecuteNonQuery();
                       }
                       catch
                       { }
                       finally
                       {
                           con.Close();
                           con.Dispose();
                           cmd.Dispose();
                       }
                       MessageBox.Show("修改成功!");
                             //刷新数据
                       DataSet dss = new DataSet();
                       sql = "select * from telephone order by id";
                       SqlDataAdapter sda = new SqlDataAdapter(sql,conn);
                       da.Fill(ds,"telephone");
                       if (ds.Tables["telephone"].Rows.Count > 0)
                       {
                           this.dataGridView1.DataSource = ds.Tables["telephone"];
                       }
                   }
                }
            }
            catch
            {}
            
sql =string .Format ( "select * from telephone where name={0}",this.textBox1 .Text.Trim ());
sql = string.Format(@"update telephone set mb='{0}',ph='{1}',unit='{2}',addr='{3}'where name='{4}'",this .textBox2 .Text .Trim (),
                           this.textBox3.Text.Trim(), this.textBox4.Text.Trim(), this.textBox5.Text.Trim(), this.textBox1.Text.Trim());


现在试试看。。。

[ 本帖最后由 girl0001 于 2011-10-23 15:47 编辑 ]
2011-10-23 15:45
转角之后
Rank: 2
来 自:山东
等 级:论坛游民
帖 子:11
专家分:18
注 册:2011-10-13
收藏
得分:15 
楼上的代码应该可以.
你的代码写的也太乱了,如果把SQL语句跟数据库连接,操作语句分开写差错时会更清晰,建议写一个帮助类。
2011-10-24 14:58
快速回复:C#修改数据
数据加载中...
 
   



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

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