日期:2014-05-17  浏览次数:20714 次

求助啊,,老是报错啊~~~
 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
            {