我建了T_Reader表和视图V_Reader,V_Reader从T_Reader表和其它几张表中取得。用update更新T_Reader表,没有报错,但是查看数据库发现并没有更新,我觉得和建立了视图有关。
代码如下
try
{
using (SqlCommand cmd = db.CreateCommand())
{
cmd.CommandText = "update T_Reader set T_Reader.Tel = '"+txb_tel.Text+"', T_Reader.QQ= '"+txb_qq.Text+"', T_Reader.Email= '"+txb_email.Text+"' where T_Reader.No= '"+logNo+"'";
cmd.ExecuteNonQuery();
}
}
catch (SqlException sex)
{
MessageBox.Show("数据库更新失败:" + sex.Message, "消息");
}
MessageBox.Show("联系方式修改成功!", "消息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
注:db是我自己写的连接数据库类,里面包含了SqlConnection对象和相应的CreateCommand方法
数据库视图
代码如下
try
{
using (SqlCommand cmd = db.CreateCommand())
{
cmd.CommandText = "update T_Reader set T_Reader.Tel = '"+txb_tel.Text+"', T_Reader.QQ= '"+txb_qq.Text+"', T_Reader.Email= '"+txb_email.Text+"' where T_Reader.No= '"+logNo+"'";
cmd.ExecuteNonQuery();
}
}
catch (SqlException sex)
{
MessageBox.Show("数据库更新失败:" + sex.Message, "消息");
}
MessageBox.Show("联系方式修改成功!", "消息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
注:db是我自己写的连接数据库类,里面包含了SqlConnection对象和相应的CreateCommand方法
数据库视图
db.CreateCommand()实际上是调标准的,也就是.net framework里面的,所以不会错的~
class DbHelper
{
private SqlConnection conn;
private string connStr="";
/// <summary>
/// ConnStr属性,用来返回连接字符串的值,如果连接字符串为空,会抛出NullReferenceException。
/// </summary>
public string ConnStr
{
get
{
if (this.connStr == "")
throw new NullReferenceException("连接字符串未初始化,请先使用Open函数!");
else
return this.connStr;
}
} public SqlConnection Connection
{
get { return this.conn; }
}
public void Open()
{
//获取config文件中的连接字符串
connStr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
//创建一个连接
conn = new SqlConnection(connStr);
//打开连接
conn.Open();
} public void Close()
{
this.conn.Close();
//因为SqlConnection实现了IDispose接口,所以要调用Dispose()释放资源
this.conn.Dispose();
} /// <summary>
/// 创建一个连接并返回
/// </summary>
/// <returns></returns>
public SqlCommand CreateCommand()
{
return conn.CreateCommand();
}
}
所以它也不会报错。你把看看返回的值是多少。是否大于1.
你确保数据库的sql语句,跟界面调用的是同一个吗?界面层有没有什么地方修改了qq?这种毫无根据的猜测,往往是徒劳的。最好自己单步调试,一步步的接近“真相”。