这个运行不出来,请问是哪里出错了?

public static class SqlHelper
{
1 string connStr = @"Data Source =.; Initial catalog=Myshop;Integrated Security = True";
2 public static object ExecuteScalarstring sql)
{
3 object obj = null;
4 using SqlConnection conn = new SqlConnectionconnStr ()
{
5 using SqlCommand cmd = new SqlCommand(conn,sql))
{
6 try
{
7 conn.close();
8 obj=cmd.ExecuteScalar);
9 return obj;
}
10 catch exception (ex)
{
11 throw ex;
}
}
}
}

这是按你的要求修改的完整代码

  public static class SqlHelper
    {
        static string connStr = @"Data Source =.; Initial catalog=Myshop;Integrated Security = True";
        public static object ExecuteScalar(string sql)
        {
            object obj = null;
            using (SqlConnection conn = new SqlConnection(connStr))
            {

                using (SqlCommand cmd = new SqlCommand(sql, conn))
                {
                    try
                    {
                        
                        conn.Close();
                        obj = cmd.ExecuteScalar();
                        return obj;
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
            }
        }
    }

你好,
你在执行查询之前就把连接关闭了,当然会有问题了!
步骤:

       SqlConnection cnn = new SqlConnection();
        cnn.ConnectionString = "Data Source =.; Initial catalog=Myshop;Integrated Security = True";//依次为,ip,数据库名称,用户名,用户密码
        String sql = " select *  from  dbo.SACC_NEU_department_record";//需要执行的SQL语句
        cnn.Open();//连接到数据库
        SqlCommand cmd = new SqlCommand(sql, cnn); //执行连接和查询的命令
        int count = int.Parse(cmd.ExecuteScalar().ToString());
        cnn.Close();//关闭数据库连接