关于c#的winform程序和slq数据库的问题
我在我的一个管理系统中添加了一个基于服务的数据库(slq2005的),数据库中有一个Attributes表,表中只有三个字段(Att_Color,Att_Style和一个id【自动增长】),string sql = "insert into Attributes(Att_Color,Att_Style) values('" + color.Text + "','" + yangshi.Text + "')";
sqlhelper.connection();//连接数据苦的方法
SqlCommand cmd = new SqlCommand(sql, sqlhelper.conn);
if ((int)cmd.ExecuteNonQuery() > 0)
{
MessageBox.Show("chenggong");
}
执行完毕后 没有提示任何错误 而且断点调试也没有错误,可是打开数据库后没有插入的数据,更新/删除数据库也是一样。(在SQL server management 中执行insert、update就可以。)
但是如果是查询(select)的话,就可以查出数据。
不知有没有遇到这种情况的,还请高手指教一下, --------------------编程问答-------------------- 代码改为:
int i = (int)cmd.ExecuteNonQuery();
MessageBox.Show(i.ToString());
if (i > 0)
{
MessageBox.Show("chenggong");
}
看看i的值是多少?如果是1就表示更新成功了,如果不是的话检查你的sqlhelper.cs类,肯定是你用的那个类有问题! --------------------编程问答-------------------- int i=(int)((int)cmd.ExecuteNonQuery();看看 --------------------编程问答-------------------- 捕获一下异常 --------------------编程问答-------------------- C#
public override int ExecuteNonQuery()
返回值
类型:System..::.Int32
受影响的行数。
实现
IDbCommand..::.ExecuteNonQuery()()()
异常
((int)cmd.ExecuteNonQuery() 何必多此一举呢
--------------------编程问答-------------------- 看一看sqlhelper.conn这个东西是否正确。 --------------------编程问答-------------------- + --------------------编程问答-------------------- 关键是这一句
sqlhelper.connection();//连接数据苦的方法
好好检查一下。 --------------------编程问答-------------------- string sql = "insert into Attributes(Att_Color,Att_Style) values('" + color.Text + "','" + yangshi.Text + "')";
sqlhelper.connection();//连接数据苦的方法
SqlCommand cmd = new SqlCommand(sql, sqlhelper.conn);
int i=int(cmd.executnonquery().tostring());
if (i > 0)
{
MessageBox.Show("chenggong");
}
--------------------编程问答-------------------- 我建议你几点
1.sql 语句不要拼接
2.sqlhelper.connection 返回什么?连接串吗?
如果是连接串为什么没有open();
3.(int)cmd.ExecuteNonQuery() 这是什么意思?cmd.ExecuteNonQuery() 本身返回就是int类型
4.我想知道你怎么看的数据库?直接打开吗?最好还是在sqlserver中用语句查询 --------------------编程问答-------------------- 不太明白
问题的保姆,帖子的管家——《Csdn收音机》!
补充:.NET技术 , C#