读书人

小毛病帮忙看一下,该怎么解决

发布时间: 2011-12-30 23:30:45 作者: rapoo

小毛病,帮忙看一下
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
{
}
}
}


[解决办法]
报什么错了吗?把
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);
试试。
[解决办法]
SqlCommand cmd1 = new SqlCommand( "update TableHotKey set HotKey = \ ' " + TextBoxHotKey1.Text.Trim() + "\ ' where ID = 1 ", sqlCon);


to

SqlCommand cmd1 = new SqlCommand(@ "update TableHotKey set HotKey = ' " + TextBoxHotKey1.Text.Trim() + " ' where ID = 1 ", sqlCon);

试试,不行就用参数

[解决办法]
试试指定cmd1.commandtype=commandtext
------解决方案--------------------


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);

读书人网 >C#

热点推荐