| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 949 人关注过本帖
标题:C#修改数据
取消只看楼主 加入收藏
zx10283
Rank: 2
等 级:论坛游民
帖 子:25
专家分:82
注 册:2011-10-20
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:0 
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
快速回复:C#修改数据
数据加载中...
 
   



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

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