读书人

啊老是报错啊

发布时间: 2012-12-20 09:53:21 作者: rapoo

求助啊,,老是报错啊~~~
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=PHOEBE-PC;Initial Catalog=Mr_Liu_cs_design;Integrated Security=True";
SqlCommand cmd = new SqlCommand();
cmd.CommandText = string.Format("select COUNT(8)from Mr_Liu_class2_type where t_type='{0}';", temp);
cmd.Connection = con;
con.Open(); //打开数据库连接
int count = Convert.ToInt32(cmd.ExecuteScalar());
if (count > 0)
{
MessageBox.Show("添加计量单位已存在");
}
else
{
cmd.CommandText = string.Format("insert into Mr_Liu_class2_type('{0}')", temp);
cmd.ExecuteNonQuery();//用ExecuteNonQuery()的方法来执行查询语句
MessageBox.Show("添加成功");
}
con.Close(); //关闭数据库的连接
[最优解释]


string c = "Data Source=PHOEBE-PC;Initial Catalog=Mr_Liu_cs_design;Integrated Security=True";
using(SqlConnection con = new SqlConnection(c)
)
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = string.Format("select COUNT(8)from Mr_Liu_class2_type where t_type='{0}';", temp);
cmd.Connection = con;
con.Open(); //打开数据库连接
int count = Convert.ToInt32(cmd.ExecuteScalar());
if (count > 0)
{
MessageBox.Show("添加计量单位已存在");
}
else
{
cmd.CommandText = string.Format("insert into Mr_Liu_class2_type('{0}')", temp);


cmd.ExecuteNonQuery();//用ExecuteNonQuery()的方法来执行查询语句
MessageBox.Show("添加成功");
}


}


楼主代码这样改下
[其他解释]
还有 在链接语句前边 加个 @Data Source=.;database=Mr_Liu_cs_design;Integrated Security=True";
[其他解释]

cmd.CommandText = string.Format("select COUNT(8)from Mr_Liu_class2_type where t_type='{0}';", temp);

cmd.CommandText = string.Format("insert into Mr_Liu_class2_type('{0}')", temp);

这两个temp可以共用??
[其他解释]
感觉还落了很多知识点!!!
[其他解释]
哪一行?报什么错?
[其他解释]
请问楼主 哪里报错了?
COUNT(8) from 聚合函数和FROM中间 加个空格试试
[其他解释]
temp是什么
[其他解释]
错误是 未将对象引用设置到对象的实例?

temp 从哪里声明的,又是从哪里定义的呢?
[其他解释]
temp是啥子
[其他解释]
引用:
temp是什么
前面有定义。
string temp;
temp = this.textBox1.Text;
用来装box的字符串的。
[其他解释]
引用:
感觉还落了很多知识点!!!
求指教
[其他解释]
引用:
cmd.CommandText = string.Format("select COUNT(8)from Mr_Liu_class2_type where t_type='{0}';", temp);

cmd.CommandText = string.Format("insert into Mr_Liu_class2_type('{0}')", temp);

这两个temp可以共用……
我没共用还是报错,是类型未经处理异常
[其他解释]
问题是你这报什么错
[其他解释]
引用:
C# code?1234567891011121314151617181920212223string c = "Data Source=PHOEBE-PC;Initial Catalog=Mr_Liu_cs_design;Integrated Security=True";using(SqlConnection con = new SqlConnection(c)){ ……
呵呵我找到错误时我sql语句错了!!很感谢您们~~
[其他解释]
引用:
C# code?1234567891011121314151617181920212223string c = "Data Source=PHOEBE-PC;Initial Catalog=Mr_Liu_cs_design;Integrated Security=True";using(SqlConnection con = new SqlConnection(c)){ ……
感觉还是自己写东西不够规范~~才会漏了东西
[其他解释]
友情提示,楼主如果报错,请贴出错误信息,也可以自己设置断点调试,调试是最好的学习

读书人网 >C#

热点推荐