using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;using System.Data.SqlClient;public partial class Admin : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //读取关键字
        try
        {
            string strCon = "Data Source =(local);Integrated Security = SSPI;Initial Catalog = EduDB;";            SqlConnection sqlCon = new SqlConnection(strCon);
            sqlCon.Open();
            //key1
            SqlCommand cmdHotKey1 = new SqlCommand("SELECT HotKey FROM [TableHotkey] where ID = 1", sqlCon);
            TextBoxHotKey1.Text = Convert.ToString(cmdHotKey1.ExecuteScalar());
            //key2
            SqlCommand cmdHotKey2 = new SqlCommand("select HotKey from TableHotKey where ID = 2", sqlCon);
            TextBoxHotKey2.Text = Convert.ToString(cmdHotKey2.ExecuteScalar());
            //key3
            SqlCommand cmdHotKey3 = new SqlCommand("select HotKey from TableHotKey where ID = 3", sqlCon);
            TextBoxHotKey3.Text = Convert.ToString(cmdHotKey3.ExecuteScalar());
            //key4
            SqlCommand cmdHotKey4 = new SqlCommand("select HotKey from TableHotKey where ID = 4", sqlCon);
            TextBoxHotKey4.Text = Convert.ToString(cmdHotKey4.ExecuteScalar());
            //key5
            SqlCommand cmdHotKey5 = new SqlCommand("select HotKey from TableHotKey where ID = 5", sqlCon);
            TextBoxHotKey5.Text = Convert.ToString(cmdHotKey5.ExecuteScalar());            sqlCon.Close();
        }
        catch
        {
        }
        
    }
    protected void ButtonHotKey_Click(object sender, EventArgs e)
    {
        try
        {
            string strCon = "Data Source =(local);Integrated Security = SSPI;Initial Catalog = EduDB;";
            SqlConnection sqlCon = new SqlConnection(strCon);
            sqlCon.Open();            string hk1 = TextBoxHotKey1.Text.Trim();
            SqlCommand cmd1 = new SqlCommand("update TableHotKey set HotKey = \'" + TextBoxHotKey1.Text.Trim() + "\' where ID = 1", sqlCon);
            cmd1.ExecuteNonQuery();
            //这为什么不更新数据呢
            sqlCon.Close();
        }
        catch
        {
        }
    }
}

解决方案 »

  1.   

    报什么错了吗?把
    SqlCommand cmd1 = new SqlCommand("update TableHotKey set HotKey = \'" + TextBoxHotKey1.Text.Trim() + "\' where ID = 1", sqlCon);
    这句改成
    SqlCommand cmd1 = new SqlCommand("update TableHotKey set HotKey = '" + TextBoxHotKey1.Text.Trim() + "' where ID = 1", sqlCon);
    试试。
      

  2.   

    试了,不报错,还是不行,同样的语句我在SQL SERVER 2000的查询分析器里用就可以,为什么在这就不行呢?
      

  3.   

    SqlCommand cmd1 = new SqlCommand("update TableHotKey set HotKey = \'" + TextBoxHotKey1.Text.Trim() + "\' where ID = 1", sqlCon);
    toSqlCommand cmd1 = new SqlCommand(@"update TableHotKey set HotKey = '" + TextBoxHotKey1.Text.Trim() + "' where ID = 1", sqlCon);试试,不行就用参数
      

  4.   

    试试指定cmd1.commandtype=commandtext
      

  5.   

    SqlCommand cmd1 = new SqlCommand("update TableHotKey set HotKey = \'" + TextBoxHotKey1.Text.Trim() + "\' where ID = 1", sqlCon);
    ====================
    单引号为什么要加转义符?把转义符去了看看
    SqlCommand cmd1 = new SqlCommand("update TableHotKey set HotKey = '" + TextBoxHotKey1.Text.Trim() + "' where ID = 1", sqlCon);
      

  6.   

    加了cmd1.CommandType = CommandType.Text;也去了\  还是不行啊。郁闷啊
      

  7.   

    要看异常信息,你catch里面什么也不做当然不报错了
      

  8.   

    SqlCommand cmd1 = new SqlCommand("update TableHotKey set HotKey = 'XX!!XX' where ID = 1", sqlCon);
    改成这样,看HotKey能不能变成XX!!XX
      

  9.   

    注意SqlCommand 是独占SqlConnection对象的
    每个是即开即用即关。不能共享的。
      

  10.   

    try
            {
                string strCon = "Data Source =(local);Integrated Security = SSPI;Initial Catalog = EduDB;";
                SqlConnection sqlCon = new SqlConnection(strCon);
                
                string hk1 = TextBoxHotKey1.Text.Trim();
                SqlCommand cmd1 = new SqlCommand("update TableHotKey set HotKey = '" +hk1 + "' where ID = 1", sqlCon);
              sqlCon.Open();            cmd1.ExecuteNonQuery();
                //这为什么不更新数据呢
                sqlCon.Close();
      

  11.   

    上面是改过的,还是不行的话,看看有没有异常,没有的话,看能不能取到正确值。cmd1.ExecuteNonQuery();
    看看有没有执行?
      

  12.   

    我找到原因了,我把程序贴出来,上面LOAD中的如果不是注释则还是不行,我一变成注释就可以了,谁知道这是为什么吗
    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;using System.Data.SqlClient;public partial class Admin : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //读取关键字
            //try
            //{
            //    string strCon = "Data Source =(local);Integrated Security = SSPI;Initial Catalog = EduDB;";        //    SqlConnection sqlCon = new SqlConnection(strCon);
            //    sqlCon.Open();
            //    //key1
            //    SqlCommand cmdHotKey1 = new SqlCommand("SELECT HotKey FROM [TableHotkey] where ID = 1", sqlCon);
            //    TextBoxHotKey1.Text = Convert.ToString(cmdHotKey1.ExecuteScalar());
            //    //key2
            //    SqlCommand cmdHotKey2 = new SqlCommand("select HotKey from TableHotKey where ID = 2", sqlCon);
            //    TextBoxHotKey2.Text = Convert.ToString(cmdHotKey2.ExecuteScalar());
            //    //key3
            //    SqlCommand cmdHotKey3 = new SqlCommand("select HotKey from TableHotKey where ID = 3", sqlCon);
            //    TextBoxHotKey3.Text = Convert.ToString(cmdHotKey3.ExecuteScalar());
            //    //key4
            //    SqlCommand cmdHotKey4 = new SqlCommand("select HotKey from TableHotKey where ID = 4", sqlCon);
            //    TextBoxHotKey4.Text = Convert.ToString(cmdHotKey4.ExecuteScalar());
            //    //key5
            //    SqlCommand cmdHotKey5 = new SqlCommand("select HotKey from TableHotKey where ID = 5", sqlCon);
            //    TextBoxHotKey5.Text = Convert.ToString(cmdHotKey5.ExecuteScalar());        //    sqlCon.Close();
            //}
            //catch
            //{
            //}
            
        }
        protected void ButtonHotKey_Click(object sender, EventArgs e)
        {
            try
            {
                string strCon = "Data Source =(local);Integrated Security = SSPI;Initial Catalog = EduDB;";
                SqlConnection sqlCon = new SqlConnection(strCon);
                sqlCon.Open();            string hk1 = TextBoxHotKey1.Text.Trim();
                SqlCommand cmd1 = new SqlCommand("update TableHotKey set HotKey = '" + TextBoxHotKey1.Text.Trim() + "' where ID = 1", sqlCon);
                cmd1.CommandType = CommandType.Text;
                cmd1.ExecuteNonQuery();
                //这为什么不更新数据呢
                sqlCon.Close();
            }
            catch
            {
                Console.WriteLine(1111);
            }   
        }
    }